www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - CTFE toUpper/toLower

reply "cal" <callumenator gmail.com> writes:
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)?
Aug 29 2012
next sibling parent reply Philippe Sigaud <philippe.sigaud gmail.com> writes:
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) ~ ", ";
Aug 29 2012
parent "cal" <callumenator gmail.com> writes:
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.
Aug 29 2012
prev sibling parent reply Jacob Carlborg <doob me.com> writes:
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
Aug 29 2012
parent reply Jacob Carlborg <doob me.com> writes:
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
Aug 29 2012
parent reply "cal" <callumenator gmail.com> writes:
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
Aug 30 2012
parent "cal" <callumenator gmail.com> writes:
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
Aug 30 2012