Bug 10728 – A combination of implicit conversion and lambda call cannot be compiled
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-07-29T10:10:00Z
Last change time
2017-06-26T14:04:24Z
Assigned to
nobody
Creator
ttanjo
Comments
Comment #0 by ttanjo — 2013-07-29T10:10:32Z
In the following code, function foo cannot be compiled but it should.
--- foo.d
string foo(char[] s) // It cannot be compiled
{
return s ? (){ return s[0..$].dup; }() : null;
}
string bar(char[] s) // It can be compiled
{
return s ? s[0..$].dup : null;
}
---
Output:
$ dmd -main -run foo.d
foo.d(3): Error: cannot implicitly convert expression (s ? delegate char[]()
{
return _adDupT(& _D11TypeInfo_Aa6__initZ, s[0LU..__dollar]);
}
() : null) of type char[] to string
Comment #1 by ttanjo — 2013-07-29T10:12:57Z
It is reproduced by dmd v2.064-devel-f1c47fb on Linux 64bit.
Comment #2 by dlang-bugzilla — 2017-06-26T14:04:24Z
In D2, use .idup to create a copy of an immutable string.
The result of .dup will implicitly convert to a string in some circumstances as a convenience feature to ease migration from D1. It works because the compiler knows that although the result of .dup is mutable, it is also unique. However, this inference is limited by design.