Bug 10915 – std.typecons.Nullable throws in writeln() if it's null
Status
RESOLVED
Resolution
FIXED
Severity
minor
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-08-27T18:00:00Z
Last change time
2015-02-18T03:42:14Z
Assigned to
nobody
Creator
monkeyworks12
Comments
Comment #0 by monkeyworks12 — 2013-08-27T18:00:44Z
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.
Comment #1 by blm768 — 2013-10-07T21:12:04Z
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 ~ ")";
}
}
}
Comment #2 by monkeyworks12 — 2014-10-05T14:28:16Z