www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 10915] New: std.typecons.Nullable throws in writeln() if it's null

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

           Summary: std.typecons.Nullable throws in writeln() if it's null
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: minor
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: monkeyworks12 hotmail.com


--- Comment #0 from monkeyworks12 hotmail.com 2013-08-27 18:00:44 PDT ---
Example code that outlines my situation:

import std.array;
import std.stdio;
import std.typecons;

class NullableRange
{
    Nullable!int[] store;

    this (Nullable!int[] maybeInts)
    {
        store = maybeInts;
    }

    bool empty() { return store.empty; }

    Nullable!int front() { return store.front; }

    void popFront() { store.popFront; }
}

void main()
{
    auto arr = [Nullable!int(0), Nullable!int(1), Nullable!int()];
    auto nr  = new NullableRange(arr);
   
//core.exception.AssertError /opt/compilers/dmd2/include/std/typecons.d(1216):
Called `get' on null Nullable!int.
    writeln(nr);
}

I think isNull() should be called before calling get() on a Nullable within
writeln(). This is proving to be very annoying if I want to print a range of
Nullable values. I suggest that something like "Nullable!int.Null" or
(Nullable!int(null)" be printed instead.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 27 2013
parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10915


blm768 gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |blm768 gmail.com


--- Comment #1 from blm768 gmail.com 2013-10-07 21:12:04 PDT ---
It should be possible to fix this with a custom toString(), right?

struct Nullable(T) {
    // ...
    string toString() {
        if(isNull()) {
            return typeof(this).stringof ~ "(null)";
        } else {
            return typeof(this).stringof ~ "(" ~ get().to!string ~ ")";
        }
    }
}

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