www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 8304] New: writeln of empty Nullables too

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8304

           Summary: writeln of empty Nullables too
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: rejects-valid
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc


--- Comment #0 from bearophile_hugs eml.cc 2012-06-26 16:30:32 PDT ---
In D you can't print an empty Nullable:


import std.stdio: writeln;
import std.typecons: Nullable;
void main() {
    Nullable!int ni;
    writeln(ni);
}


DMD 2.060alpha:

object.Exception C:\dmd2\src\phobos\std\typecons.d(1218): Enforcement failed


But I'd like something similar to Haskell, where you are allowed to print an
empty Nullable (named Maybe):

...>ghci
GHCi, version 7.0.2: ...
Prelude> import Data.Maybe
Prelude Data.Maybe> Nothing :: Maybe Int
Nothing


In D when the Nullable is empty I'd like writeln to print something like
"EmptyNullable".


As temporarily workaround I've added this method to Nullable, but it's not a
good general solution:

    string toString() const
    {
        return this.isNull ? "EmptyNullable": text(get());
    }

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 26 2012
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8304



--- Comment #1 from bearophile_hugs eml.cc 2013-03-19 19:43:12 PDT ---
In DMD 2.063alpha if you print a const null Nullable:


import std.stdio: writeln;
import std.typecons: Nullable;
void main() {
    auto a = Nullable!int(10);
    writeln(a);
    auto b = const(Nullable!int)(10);
    writeln(b);
    const(Nullable!int) c;
    writeln(c);
    Nullable!int d;
    writeln(d);
}


You get:


10
const(Nullable!(int))(10, false)
const(Nullable!(int))(0, true)
==> core.exception.AssertError C:\dmd2\src\phobos\std\typecons.d(1212): Called
`get' on null Nullable!int.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 19 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8304


Andrej Mitrovic <andrej.mitrovich gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich gmail.com


--- Comment #2 from Andrej Mitrovic <andrej.mitrovich gmail.com> 2013-03-19
19:53:36 PDT ---
This could work:

    string toString() const
    {
        return this.isNull ?
            format("Nullable!%s is null", T.stringof) : text(get());
    }

How sophisticated does this have to be anyway?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 19 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8304



--- Comment #3 from bearophile_hugs eml.cc 2013-03-19 20:34:06 PDT ---
(In reply to comment #2)

 How sophisticated does this have to be anyway?

Sorry, I don't remember why I have written "but it's not a good general solution:" :-)
 This could work:
 
     string toString() const
     {
         return this.isNull ?
             format("Nullable!%s is null", T.stringof) : text(get());
     }
 

But I have two notes and half: 1) Maybe using a sink in toString is a bit more efficient. 2) Think about printing an array of nullables, do you like this? [Nullable!int is null, 55, Nullable!int is null, 22] This looks a bit better: [Nullable!int(), 55, Nullable!int(), 22] 2b) But what's even better is something similar to Python, that uses __str__ and __repr__ if you print an item or if you print a collection of items. So if you print a single Nullable you get: 55 or: Nullable!int() If you print an array/range of nullables you get: [Nothing, 55, Nothing, 22] D already does that in some cases: writeln("hello"); writeln(["hello"]); It outputs: hello ["hello"] In the first case it doesn't show the "". -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Mar 19 2013