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

  • __FILE__ workman via Digitalmars-d-learn
    • Re: __FILE__ Adam D Ruppe via Digitalmars-d-learn
      • Re: __FILE__ Stefan Koch via Digitalmars-d-learn
        • Re: __FILE__ Steven Schveighoffer via Digitalmars-d-learn
          • Re: __FILE__ Ali Çehreli via Digitalmars-d-learn
            • Re: __F... Steven Schveighoffer via Digitalmars-d-learn
    • Re: __FILE__ Mathias LANG via Digitalmars-d-learn

Reply via email to