Bug 8304 – writeln of empty Nullables too

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-06-26T16:30:32Z
Last change time
2019-07-13T15:37:19Z
Keywords
bootcamp, rejects-valid
Assigned to
No Owner
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2012-06-26T16:30:32Z
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()); }
Comment #1 by bearophile_hugs — 2013-03-19T19:43:12Z
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.
Comment #2 by andrej.mitrovich — 2013-03-19T19:53:36Z
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?
Comment #3 by bearophile_hugs — 2013-03-19T20:34:06Z
(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 "".
Comment #4 by monkeyworks12 — 2014-10-08T11:59:17Z
*** Issue 10915 has been marked as a duplicate of this issue. ***
Comment #5 by bearophile_hugs — 2014-10-31T20:37:05Z
This has improved the situation: https://github.com/D-Programming-Language/phobos/pull/2587 But the example in comment #1 still fails: void main() { import std.stdio: writeln; import std.typecons: Nullable; const(Nullable!int) c; writeln(c); } core.exception.AssertError@C:\dmd2\src\phobos\std\typecons.d(1529): Called `get' on null Nullable!int.
Comment #6 by bearophile_hugs — 2014-10-31T20:38:23Z
*** Issue 10915 has been marked as a duplicate of this issue. ***
Comment #7 by tobias — 2019-07-13T15:37:19Z
import std.stdio; import std.typecons; void main() { Nullable!string s; writeln(s); } now prints "Nullable.null".