Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
August 30, 2012 CTFE toUpper/toLower | ||||
---|---|---|---|---|
| ||||
Given this code for CTFE on a string array: enum E {one, two} enum string[] fields = [EnumMembers!E].to!(string[]); string print(string[] fields) { string s; foreach(string field; fields) { s ~= field.toUpper ~ ", "; } return s; } pragma(msg, print(["one", "two"])); pragma(msg, print(fields)); I am trying to understand why the first pragma produces correct output but the second one fails in std.string.toUpper (cannot interpret result ~= c at compile-time)? |
August 30, 2012 Re: CTFE toUpper/toLower | ||||
---|---|---|---|---|
| ||||
Posted in reply to cal | On Thu, Aug 30, 2012 at 2:13 AM, cal <callumenator@gmail.com> wrote:
> Given this code for CTFE on a string array:
>
> enum E {one, two}
> enum string[] fields = [EnumMembers!E].to!(string[]);
>
> string print(string[] fields)
> {
> string s;
> foreach(string field; fields)
> {
> s ~= field.toUpper ~ ", ";
> }
> return s;
> }
>
> pragma(msg, print(["one", "two"]));
> pragma(msg, print(fields));
>
> I am trying to understand why the first pragma produces correct output but the second one fails in std.string.toUpper (cannot interpret result ~= c at compile-time)?
>
And with no UFCS ? Did you try s ~= toUpper(field) ~ ", ";
|
August 30, 2012 Re: CTFE toUpper/toLower | ||||
---|---|---|---|---|
| ||||
Posted in reply to Philippe Sigaud | On Thursday, 30 August 2012 at 05:26:52 UTC, Philippe Sigaud wrote:
> And with no UFCS ? Did you try s ~= toUpper(field) ~ ", ";
Yeah with or without UFCS, the second one fails.
|
August 30, 2012 Re: CTFE toUpper/toLower | ||||
---|---|---|---|---|
| ||||
Posted in reply to cal | On 2012-08-30 02:13, cal wrote: > Given this code for CTFE on a string array: > > enum E {one, two} > enum string[] fields = [EnumMembers!E].to!(string[]); > > string print(string[] fields) > { > string s; > foreach(string field; fields) > { > s ~= field.toUpper ~ ", "; > } > return s; > } > > pragma(msg, print(["one", "two"])); > pragma(msg, print(fields)); > > I am trying to understand why the first pragma produces correct output > but the second one fails in std.string.toUpper (cannot interpret result > ~= c at compile-time)? It works for me. DMD 2.060 Mac OS X. -- /Jacob Carlborg |
August 30, 2012 Re: CTFE toUpper/toLower | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | On 2012-08-30 08:28, Jacob Carlborg wrote: > It works for me. DMD 2.060 Mac OS X. Oh, it does not. If I replace: enum string[] fields = [EnumMembers!E].to!(string[]); With: enum string[] fields = ["one", "two"]; It works. -- /Jacob Carlborg |
August 30, 2012 Re: CTFE toUpper/toLower | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | On Thursday, 30 August 2012 at 06:30:50 UTC, Jacob Carlborg wrote:
> On 2012-08-30 08:28, Jacob Carlborg wrote:
>
>> It works for me. DMD 2.060 Mac OS X.
>
> Oh, it does not. If I replace:
>
> enum string[] fields = [EnumMembers!E].to!(string[]);
>
> With:
>
> enum string[] fields = ["one", "two"];
>
> It works.
Yeah its weird because:
enum string[] literalFields = ["one", "two"];
enum string[] enumFields = [EnumMembers!E].to!(string[]);
pragma(msg, literalFields[0] == enumFields[0]);
prints true, but toUpper works on the first and not the second
|
August 30, 2012 Re: CTFE toUpper/toLower | ||||
---|---|---|---|---|
| ||||
Posted in reply to cal | On Thursday, 30 August 2012 at 17:38:48 UTC, cal wrote: > On Thursday, 30 August 2012 at 06:30:50 UTC, Jacob Carlborg wrote: Replacing string[] with dstring[] for EnumMembers triggers a DMD bug, so I guess the CTFE interpreter is buggy even in the string[] case. Filed http://d.puremagic.com/issues/show_bug.cgi?id=8601 |
Copyright © 1999-2021 by the D Language Foundation