Get variable symbol name that was passed to a paramater?

Arredondo arm.plus at gmail.com
Tue Oct 15 15:07:27 UTC 2019


So, 6 years later, what is the idiomatic way of doing this? Has 
there been any progress on this matter?

As far as I can tell from this thread, all proposed solutions are 
imperfect in some way, which is a shame for a language with 
"Second to none reflection".

Arredondo.

On Saturday, 9 November 2013 at 09:12:21 UTC, Rob T wrote:
> I have a template function called "inspect" that takes two 
> variables as arguments,
>
> void inspect(T)( string symbol, T value )
> {
>    writeln(symbol, " = ", value);
> }
>
> int y = 100;
>
> inspect( y.stringof, y );
>
> writes to console
>
> y = 100
>
> I am wondering if there's a way to pass only the variable and 
> have the inspect function determine the variable's symbol name 
> as passed rather than have to pass both the name and value 
> separately?
>
> void inspect(T)( T value )
> {
>    writeln( ? , " = ", value);
> }
>
>
> I've tried a template with alias parameter
>
> void inspect(alias value)()
> {
>    writeln( value.stringof , " = ", value);
> }
>
> It works except when passing a variable contained inside a 
> struct or class due to a missing "this" during evaluation, I'm 
> also worried about template bloat.
>
> I figure mixins may help, but not if it's same or less 
> convenient to use as the double entry method.
>
> Any suggestions, or is it just impossible or not worth trying?
>
> --rt


More information about the Digitalmars-d-learn mailing list