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
Comment #3 by bearophile_hugs — 2014-10-06T07:36:12Z
A dupe of Issue 8304 ?
Comment #4 by monkeyworks12 — 2014-10-08T11:59:17Z
*** This issue has been marked as a duplicate of issue 8304 ***
Comment #5 by github-bugzilla — 2014-10-31T19:03:11Z
Commits pushed to master at https://github.com/D-Programming-Language/phobos https://github.com/D-Programming-Language/phobos/commit/2c8fdeb0b0f92c24c4ffa26a4beca6bcd9a5c0ba Fix Issue 10915 - std.typecons.Nullable throws in writeln() if it's null https://github.com/D-Programming-Language/phobos/commit/2e58214d1d3feee582ca639edf7c5e2af8db365b Merge pull request #2587 from MetaLang/nullable-tostring Fix Issue 10915 - std.typecons.Nullable throws in writeln() if it's null
Comment #6 by bearophile_hugs — 2014-10-31T20:37:22Z
(In reply to github-bugzilla from comment #5) > Commits pushed to master at https://github.com/D-Programming-Language/phobos > > https://github.com/D-Programming-Language/phobos/commit/ > 2c8fdeb0b0f92c24c4ffa26a4beca6bcd9a5c0ba > Fix Issue 10915 - std.typecons.Nullable throws in writeln() if it's null > > https://github.com/D-Programming-Language/phobos/commit/ > 2e58214d1d3feee582ca639edf7c5e2af8db365b > Merge pull request #2587 from MetaLang/nullable-tostring > > Fix Issue 10915 - std.typecons.Nullable throws in writeln() if it's null Answered in Issue 8303 .
Comment #7 by bearophile_hugs — 2014-10-31T20:38:23Z
*** This issue has been marked as a duplicate of issue 8304 ***
Comment #8 by github-bugzilla — 2015-02-18T03:39:25Z