Bug 7348 – to!string(null) matches more than one template declaration
Status
RESOLVED
Resolution
FIXED
Severity
trivial
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-01-22T09:56:00Z
Last change time
2012-05-31T04:06:03Z
Keywords
pull
Assigned to
nobody
Creator
alienballance
Comments
Comment #0 by alienballance — 2012-01-22T09:56:28Z
import std.conv;
void main()
{
to!string(null);
}
/usr/include/d/std/conv.d(237): Error: template std.conv.toImpl(T,S) if (isImplicitlyConvertible!(S,T)) toImpl(T,S) if (isImplicitlyConvertible!(S,T)) matches more than one template declaration, /usr/include/d/std/conv.d(245):toImpl(T,S) if (isImplicitlyConvertible!(S,T)) and /usr/include/d/std/conv.d(924):toImpl(T,S) if (is(S : Object) && isSomeString!(T))
Comment #1 by issues.dlang — 2012-01-22T16:48:29Z
While I can see wanting this to work, I'm not sure how it could. What type is null? Is it string? A wstring? An int[]? An Object? If so, is it an Object or a class derived from Object? Etc.
And if what you want is a null string, then just assign null to the string.
What are you trying to do here?
Comment #2 by alienballance — 2012-01-23T11:58:31Z
[quote]What are you trying to do here?[/quote]
void test(A)(A param)
{
writeln("A(",text(param),") called");
}
void main() {
test(null);
}
Comment #3 by issues.dlang — 2012-01-23T13:49:20Z
Yeah. That doesn't really make sense. null could be anything that's null. And what type that is completely changes how text is instantiated. If you cast null to the type that you want, then it'll work. But null is its own type. You can't really do anything with null on its own like that. It needs be a null _something_, not just null.
Comment #4 by lovelydear — 2012-04-19T09:55:21Z
Should we close ?
Comment #5 by bearophile_hugs — 2012-04-19T15:07:52Z
(In reply to comment #4)
> Should we close ?
Don't close unless it's really unfixable in some way.
Maybe "void*" is an acceptable textual output for this.
---------------------
Related:
import std.stdio;
void main() {
writeln(null);
}
DMD 2.059 shows:
...\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(3): Error: template instance std.stdio.writeln!(typeof(null)) error instantiating
Comment #6 by lovelydear — 2012-04-19T15:52:44Z
> Don't close unless it's really unfixable in some way.
Seriously, this doesn't make any sense. It's always possible to break the language with stupid examples like this.
This is a waste of time and resources.
Comment #7 by dlang-bugzilla — 2012-04-19T16:01:56Z
I think this bug is valid. text(null) should be equal to "null".
I don't see how this falls under "breaking the language". The OP posted a plausible use case (logging the calls to a templated function), so calling this "stupid" is a bit over the line.
Comment #10 by bearophile_hugs — 2012-05-22T13:54:53Z
Now this code:
import std.conv, std.stdio;
void main() {
writeln(">", to!string(null), "<");
}
Prints:
><
But I expect something similar to:
>null<
-------------------------
This code:
import std.stdio;
void main() {
writeln(null);
}
gives:
...\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(3): Error: template instance std.stdio.writeln!(typeof(null)) error instantiating
Comment #11 by k.hara.pg — 2012-05-22T20:45:57Z
(In reply to comment #10)
[snip]
OK. I found a good reason why we should select "null" as representation of null literal instead of "".
Now,, to!string(something) is forwarded to formatValue(w, something, fmtspec).
If we select "" (empty string) as the representation, "" should be able to *unformat* as null lietral. But, 0 length input matches everywhere.
Then we would be able to unformat null value every time.
string input = "";
typeof(null) nullvalue;
formattedRead(input, "%s", &nullvalue); // read null value from empty input
formattedRead(input, "%s", &nullvalue); // ditto
formattedRead(input, "%s", &nullvalue); // ditto
// can repeat infinitely...
This is obviously strange.
Then we should select a representation for null value which has one or more length string.
So "null" is the best representation for the typeof(null) in D.
Comment #12 by dlang-bugzilla — 2012-05-22T20:47:58Z
I agree. I think this behavior is also more useful to the use case in OP's example (logging).