[Lazarus] const array syntax

Juha Manninen juha.manninen at phnet.fi
Tue Jan 12 12:28:51 CET 2010


On tiistai, 12. tammikuuta 2010 00:30:09 Alexander Klenin wrote:
> 1) Proposed syntax seems somewhat suboptimal. Historically, Pascal
> used redundancy and keywords to minimize the chance of errors and
> make diagnostics easier. In that spirit, I think some active indication
> of arbitrary upper bound is preferable to a mere omission:
> 
> IDECommandStrs: array[0..*] of TIdentMapEntry = (
> IDECommandStrs: array[0..auto] of TIdentMapEntry = (
> IDECommandStrs: array[0..end] of TIdentMapEntry = (

I would be happy with any syntax, although I understood that some other Pascal 
compilers already support syntax: array[0..]


> 3) Finally, regardless of the preceeding discussion, that
> IDECommandStrs array seems
> like a tiresome and pointless exercise in code duplication. Why not use
> 
> function IDECommandStr(ec: TIdentMapEntry): String;
> begin
>   WriteStr(ec, Result);
> end;
> 
> instead?

TIdentMapEntry is a record with Value and Name. Unfortunately "Value" is 
integer and all the constants listed in IDECommandStrs array are defined as 
const, not enum type.

"WriteStr(Result, ec.Value)" would return a numeric string like IntToStr does.

Now I tested enum type and WriteStr. It actually returns the original 
definition as string! I didn't know that. Where does the string come from 
at run-time? RTTI?

The commands in Lazarus are defined in many places, in many units. Then lookup 
functions (below) are registered (added to some list) and used later. It is 
actually rather complicated.
Using enums would make the code cleaner. It should be possible to define those 
constants in different enum types and finally cast to integer when needed.


function IdentToIDECommand(const Ident: string; var Cmd: longint): boolean;
begin
  Result := IdentToInt(Ident, Cmd, IDECommandStrs);
end;

function IDECommandToIdent(Cmd: longint; var Ident: string): boolean;
begin
  Result := IntToIdent(Cmd, Ident, IDECommandStrs);
end;


Regards,
Juha




More information about the Lazarus mailing list