[fpc-pascal] specify variable name with string variable

Santiago A. svaa at ciberpiula.net
Mon Jul 8 21:40:20 CEST 2019


El 07/07/2019 a las 21:58, James Richters escribió:
> This might sound silly,  but is it possible to somehow specify a variable with a string containing the name of the variable?
>
> For example:
>
> Var
>     MyVariable1 : Word;
>     MyVariableName : String;
>
> Procedure ShowVariable(Variablename);
> Begin
> Writeln(Variablename,' = $', inttohex(    (Somehow get the value of the variable specified by the name here ) ,4));
> End;
>
> Begin
> MyVariableName:= 'MyVariable1';
> ShowVariable(MyVariableName);
> End.
>
> Is such a thing possible?

What is it for?

What about:

procedure ShowVariable(const Variablename:string; const value:word);
begin
  Writeln(Variablename,' = $'+inttohex(value ,4));
end;

ShowVariable('myVar',myVar);


And as far as I understand, you want to call 
ShowVariable('myVar',myVar), but not wasting time writing the name 
enclosed in quotes, let the program get the identifier name in runtime, 
or the other way around, let the program get the value from a name 
passed in runtime.

Bad luck, Pascal is not a script language, it is not aware of identifier 
names in runtime there is no "eval". Thanks to that it gets rids of all 
the burden of having to keep in runtime structures to know the name, 
type and value of variable. It generate fast native programs.

Nevertheless, there are ways, you can program that in Freepascal using 
classes, properties, RTII etc. It is more flexible and powerful. But 
obviously it has a price in complexity. Instead or using the standard 
operators and reference a simple variable, you must use a more 
convoluted syntax.

As far as I see, you don't intend to let the program create new vars in 
runtime, you just want to write the identifiers of the vars you have 
hardcoded.  Is it worth?

(I hope it is not just for debugging)

-- 
Saludos

Santiago A.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20190708/d0e0ed83/attachment.html>


More information about the fpc-pascal mailing list