[Issue 8304] writeln of empty Nullables too

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Mar 19 20:34:06 PDT 2013


http://d.puremagic.com/issues/show_bug.cgi?id=8304



--- Comment #3 from bearophile_hugs at eml.cc 2013-03-19 20:34:06 PDT ---
(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 "".

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


More information about the Digitalmars-d-bugs mailing list