__FILE__

Ali Çehreli acehreli at yahoo.com
Thu Aug 5 02:27:11 UTC 2021


On 8/4/21 7:17 PM, Steven Schveighoffer wrote:

 >> The compiler has to evaluate the default argument as constant
 >> expression in order to use it as default value..
 >
 > This is not true, you can use runtime calls as default values.

Whaaaaat??? I just checked and it works! :)

string bar() {
   import std.process;
   return ("BAR" in environment) ? environment["BAR"] : null;
}

string foo(string s = bar()) {
   return s;
}

void main() {
   import std.stdio;
   writeln(foo("hello world"));
   writeln(foo());
}

Run e.g. with

$ BAR=whaaat ./deneme

I wonder whether this feature is thanks to 'lazy' parameters, which are 
actually delegates.

Ali



More information about the Digitalmars-d-learn mailing list