Thread overview
[Issue 9636] New: null initialization for std.typecons.Nullable
Jun 20, 2013
irritate
March 02, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9636

           Summary: null initialization for std.typecons.Nullable
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody@puremagic.com
        ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@eml.cc 2013-03-02 12:43:34 PST ---
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() {}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 20, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9636


irritate <irritate@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |irritate@gmail.com


--- Comment #1 from irritate <irritate@gmail.com> 2013-06-19 22:39:30 PDT ---
https://github.com/D-Programming-Language/phobos/pull/1356

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 20, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9636


monarchdodra@gmail.com changed:

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


--- Comment #2 from monarchdodra@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: -------
August 20, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9636



--- Comment #3 from bearophile_hugs@eml.cc 2013-08-20 13:01:04 PDT ---
(In reply to comment #2)

> 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.

Hopefully I'll not see such code :-)


> 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)
> {}

This seems acceptable.

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