Bug 1059 – String literal concatenated with array of chars - inconsistent behavior

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
All
OS
All
Creation time
2007-03-13T16:36:00Z
Last change time
2014-02-16T15:23:38Z
Keywords
rejects-valid
Assigned to
nobody
Creator
gavrilyak
Depends on
2391

Comments

Comment #0 by gavrilyak — 2007-03-13T16:36:28Z
char[] hi1 = "h" ~ "i"; //works char[] hi1 = "h" ~ ['i']; //works char[] hi2 = ['h'] ~ "i"; // semicolon expected, not '~' //found '~' instead of statement char[] hi3 = ['h'] ~ ['i'];// semicolon expected, not '~' //found '~' instead of statement All 4 case means the same and should behave the same way.
Comment #1 by gavrilyak — 2007-03-13T16:44:53Z
Update: This issue occurs only in initialization, normal assignment works. char[] hi = "h" ~ "i"; //works char[] hi1 = "h" ~ ['i']; //works char[] hi2 = ['h'] ~ "i"; // semicolon expected, not '~' //found '~' instead of statement char[] hi3 = ['h'] ~ ['i'];// semicolon expected, not '~' //found '~' instead of statement hi1 = "h" ~ ['i']; //works hi1 = ['h'] ~ "i"; //works hi1 = ['h'] ~ ['i']; //works
Comment #2 by matti.niemenmaa+dbugzilla — 2008-08-23T05:29:04Z
Annoying because this limits the use of array literals in templates, A doesn't compile in the following: //template A(char a, char b) { const char[] A = [a] ~ [b]; } template B(char[] a, char[] b) { const char[] B = a ~ b ; } void main() { // static assert (A!('a', 'b') == "ab"); static assert (B!("a", "b") == "ab"); static assert (['a'] ~ ['b'] == "ab"); }
Comment #3 by matti.niemenmaa+dbugzilla — 2008-08-23T10:45:02Z
The template issue can be worked around as well, replace [a,b,...] with (cast(typeof(a))[] ~ a ~ b ~ ...): template A(char a, char b) { const char[] A = cast(char[])[] ~ a ~ b; }
Comment #4 by matti.niemenmaa+dbugzilla — 2008-08-23T11:33:08Z
This one I haven't found a workaround for: template Id(char[] s) { const Id = s; } // succeed static assert (Id!("x" ~ "y") == "xy"); static assert (Id!(['x'] ~ ['y']) == "xy"); // fail static assert (Id!("x" ~ ['y']) == "xy"); static assert (Id!(['x'] ~ "y") == "xy"); The second two give errors of the form: asdf.d(7): Error: expression "x" ~ ['y'] is not a valid template value argument asdf.d(7): template instance asdf.Id!("x" ~ ['y']) error instantiating asdf.d(7): static assert ("x" ~ ['y'] == "xy") is not evaluatable at compile time
Comment #5 by yebblies — 2011-06-10T04:53:16Z
These cases work in the current compilers. (dmd1.068 & dmd2.053)