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



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



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