static foreach / How to construct concatenated string?

Robert M. Münch robert.muench at saphirion.com
Sun Mar 8 20:28:01 UTC 2020


On 2020-03-07 16:41:47 +0000, MoonlightSentinel said:

> You can use an anonymous lambda to build the string in CTFE:
> 
> ------------------------------------------
> 
> struct S {
>      int a;
>      bool b;
> }
> 
> import std;
> 
> enum string sql = {
>      string s = "CREATE TABLE data(";
> 
>      static foreach(f; FieldNameTuple!S) {
>      	s ~= f ~ ",";
>      }
> 
> 	s ~= ");";
>      return s;
> } ();
> 
> pragma(msg, sql);
> 
> ------------------------------------------
> 
> This prints "CREATE TABLE data(a, b);"

You can get rid of the enum und the static and it will work too.

-- 
Robert M. Münch
http://www.saphirion.com
smarter | better | faster



More information about the Digitalmars-d-learn mailing list