Source expression passed to a lazy parameter

Stefan Koch uplink.coder at googlemail.com
Mon Apr 9 13:39:29 UTC 2018


On Monday, 9 April 2018 at 08:27:50 UTC, Per Nordlöw 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

Because of the way D works with a given piece of code may not 
have a source-location or even a representation which is valid D 
source code.

Note: There is a way to fix this but it's very involved.

Step 1: you use cow (copy-on-write) when modifying AST nodes in 
semantic() or you keep distinct trees.
Step 2: you sanitize implicitly generated code to make sure it's 
actually valid code.
Step 3: you write the generated code, to a well-defined location 
such that source-of can point to a valid location.

also note that support for sourceof at compiletime will bloat the 
executable since it needs to store the source-text.


More information about the Digitalmars-d-learn mailing list