Optional parameters?
Cym13
cpicard at openmailbox.org
Mon Apr 2 10:24:32 UTC 2018
On Monday, 2 April 2018 at 09:31:35 UTC, Timoses wrote:
> On Sunday, 1 April 2018 at 15:54:16 UTC, Steven Schveighoffer
> wrote:
>> 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
>> }
>
>
> Can somebody enlighten me what this topic is about?
>
> I thought an optional parameter would be as easy as
>
> void foo(int i = 0) { writeln(i); }
>
> void main()
> {
> int x;
> foo(x);
> foo(1);
> foo();
> }
>
> Is the Nullable!int approach because 'i' would always
> "optionally" be 0 if not passed with above 'foo'?
Same feeling here, this situation really asks for a Null Object
pattern, not nullable. It's sad that nullable isn't very good in
that situation but trying to force it in place doesn't seem very
reasonnable.
More information about the Digitalmars-d-learn
mailing list