Source expression passed to a lazy parameter

Jonathan M Davis newsgroup.d at jmdavisprog.com
Mon Apr 9 08:55:02 UTC 2018


On Monday, April 09, 2018 08:27:50 Per Nordlöw via Digitalmars-d-learn 
wrote:
> Is it possible to get the source expression sent to a lazy
> function?
>
> So that I can implement something like
>
> show(Arg)(lazy Arg arg)
> {
>      writeln(arg.sourceof, arg);
> }
>
> used as
>
>      show(1+2+3);
>
> will print
>
>      1+2+3:6

Given how lazy parameters work, I don't see how that would be possible. lazy
parameters are implemented as delegates, and the function doesn't have
access to what the argument was, just the delegate to get the result of
evaluating it. Functions with lazy parameters don't have to be templates,
and if the function is a template, as long as the type of the expression is
the same, it should only result in one instantiation of the function
template. As such, the only way that something like what you're suggesting
would work would be if there were some way to get the string representation
of the body of a delegate at runtime, and that would be pretty much the same
as getting the string representation of any function at runtime. That sort
of thing simply isn't kept around beyond what's required to compile the
function in the first place. The closest that you'd get would be whatever
comes with debug symbols when they're enabled.

- Jonathan M Davis




More information about the Digitalmars-d-learn mailing list