Optional parameters?

Steven Schveighoffer schveiguy at yahoo.com
Sun Apr 1 15:54:16 UTC 2018


I currently have a situation where I want to have a function that 
accepts a parameter optionally.

I thought maybe Nullable!int might work:

void foo(Nullable!int) {}

void main()
{
    foo(1); // error
    int x;
    foo(x); // error
}

Apparently, I have to manually wrap an int to get it to pass. In other 
languages that support optional types, I can do such things, and it 
works without issues.

I know I can do things like this:

void foo(int x) { return foo(nullable(x)); }

But I'd rather avoid such things if possible. Is there a way around 
this? Seems rather limiting that I can do:

Nullable!int x = 1;

but I can't implicitly convert 1 to a Nullable!int for function calls.

-Steve


More information about the Digitalmars-d-learn mailing list