Comment #0 by bearophile_hugs — 2012-05-04T12:55:24Z
Problem found by Ali Çehreli.
D2 code:
import std.stdio: writeln;
class Foo {}
struct Bar {}
void main() {
Foo f = null;
writeln(f);
Bar* b = null;
writeln(b);
writeln(null);
}
Expected output similar to:
cast(Foo)null
cast(Bar*)null
null
DMD 2.056 prints "null" in the first two writeln, and gives errors for the third one:
...\dmd2\src\phobos\std\stdio.d(1562): Error: undefined identifier 'length'
...\dmd2\src\phobos\std\stdio.d(1562): Error: undefined identifier 'ptr', did you mean 'template tr(C1,C2,C3,C4 = immutable(char))'?
test.d(9): Error: template instance std.stdio.writeln!(typeof(null)) error instantiating
Comment #2 by bearophile_hugs — 2012-05-23T03:01:57Z
(In reply to comment #1)
> https://github.com/D-Programming-Language/phobos/pull/599
>
> With my pull, original code output is:
> ----
> null
> null
> null
Thank you Kenji Hara, this is an improvement.
But isn't it better to add types (as in cast(Foo) or cast(Bar*)) to better tell apart the three kinds of null in that example?
Comment #3 by k.hara.pg — 2012-05-23T03:26:04Z
(In reply to comment #2)
> (In reply to comment #1)
> But isn't it better to add types (as in cast(Foo) or cast(Bar*)) to better tell
> apart the three kinds of null in that example?
No, it isn't. It is consistent with other literal formatting.
writeln(1); // prints "1", not "cast(int)1"
writeln(1L); // prints "1", not "cast(long)1"
writeln(1f); // prints "1", not "cast(float)1"
And, formatting as like cast syntax will make unfomatting complicate. It is less benefit.
Furthermore, std.format module does not print value type in general.
(Deprecated typedef value was formatted like cast syntax, but I think it is special case)
Therefore, I think your proposal has many faults than benefits.
Comment #4 by bearophile_hugs — 2012-05-23T04:07:37Z
(In reply to comment #3)
> writeln(1); // prints "1", not "cast(int)1"
> writeln(1L); // prints "1", not "cast(long)1"
> writeln(1f); // prints "1", not "cast(float)1"
In my opinion writeln(1f) and writeln(1.0L) should print 1.0, as in Python:
>>> int(1)
1
>>> float(1)
1.0
> And, formatting as like cast syntax will make unfomatting complicate. It is
> less benefit.
>
> Furthermore, std.format module does not print value type in general.
> (Deprecated typedef value was formatted like cast syntax, but I think it is
> special case)
>
> Therefore, I think your proposal has many faults than benefits.
OK. Thank you for the answers.
Comment #5 by k.hara.pg — 2012-05-29T00:55:57Z
*** Issue 7866 has been marked as a duplicate of this issue. ***