[Issue 9636] null initialization for std.typecons.Nullable

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Aug 20 09:33:48 PDT 2013


http://d.puremagic.com/issues/show_bug.cgi?id=9636


monarchdodra at gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |monarchdodra at gmail.com


--- Comment #2 from monarchdodra at gmail.com 2013-08-20 09:33:46 PDT ---
(In reply to comment #0)
> Currently if I want a Nullable function argument initialized to null I have to
> use:
> 
> import std.typecons: Nullable;
> void foo(Nullable!(immutable int[4]) items = Nullable!(immutable int[4]).init)
> {}
> void main() {}
> 
> 
> Or with a global helper alias:
> 
> import std.typecons: Nullable;
> alias NullableItems = Nullable!(immutable int[4]);
> void foo(NullableItems items = NullableItems.init) {}
> void main() {}
> 
> 
> But maybe there is a way to modify std.typecons.Nullable so this simpler code
> is accepted (or something equally simple):
> 
> import std.typecons: Nullable;
> void foo(Nullable!(immutable int[4]) items = null) {}
> void main() {}

I don't think that is acceptable, as you will change the behavior of
Nullable!T, if "t = null" already meant something. EG:

//----
import std.typecons;

void main()
{
    auto n = Nullable!(int*)(null);
    assert(!n.isNull);
    assert(n.get() == null);
}
//----

Arguably, you won't see that very often, but it is plausible for someone to
want to be able to have a nullable pointer, whose "non-null" value can itself
be null.

As a workaround, Nullable-specific "null-token" could work? EG: something along
the lines of:

EG:
enum Nullable {Null}

void foo(Nullable!(immutable int[4]) items = Nullable.Null)
{}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list