Bug 2082 – Cannot convert char[] to string

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Linux
Creation time
2008-05-08T17:10:00Z
Last change time
2015-06-09T01:14:38Z
Assigned to
bugzilla
Creator
jlquinn

Comments

Comment #0 by jlquinn — 2008-05-08T17:10:53Z
This is really ugly. The following program: void test(char[] s) { string t = s; } gives the following error: junk2.d(2): Error: cannot implicitly convert expression (s) of type char[] to invariant(char)[] I haven't found a way around this yet. It really should be handled as an implicit conversion, with a copy being made under the covers as needed.
Comment #1 by lovesyao — 2008-05-08T17:15:53Z
Reply to [email protected], > http://d.puremagic.com/issues/show_bug.cgi?id=2082 > > Summary: Cannot convert char[] to string > Product: D > Version: 2.012 > Platform: PC > OS/Version: Linux > Status: NEW > Severity: normal > Priority: P2 > Component: DMD > AssignedTo: [email protected] > ReportedBy: [email protected] > This is really ugly. The following program: > > void test(char[] s) { > string t = s; > } > gives the following error: > > junk2.d(2): Error: cannot implicitly convert expression (s) of type > char[] to invariant(char)[] > > I haven't found a way around this yet. It really should be handled as > an implicit conversion, with a copy being made under the covers as > needed. > I think you need to use .idup
Comment #2 by jlquinn — 2008-05-08T17:23:14Z
To me, this seems like it's going to clutter commonly occurring code. Since char[] and string are similar, and the language is generally trying to treat them like built-in types, I would hope this kind of conversion was transparent most of the time.
Comment #3 by kamm-removethis — 2008-05-09T01:02:31Z
This is desired behaviour. Use idup() to make a new invariant copy or use a cast (via assumeUnique from std.contracts?) if you can guarantee the data is invariant.
Comment #4 by kamm-removethis — 2008-05-09T01:03:00Z
*** Bug 2083 has been marked as a duplicate of this bug. ***