digitalmars.D.learn - static foreach / How to construct concatenated string?
- =?iso-8859-1?Q?Robert_M._M=FCnch?= (14/14) Mar 07 2020 I want to create a "CREATE TABLE data (...)" where the columns are
- Adam D. Ruppe (5/8) Mar 07 2020 Use regular foreach with a regular string. Put that inside a
- =?iso-8859-1?Q?Robert_M._M=FCnch?= (7/11) Mar 07 2020 Perfect! This implicit CTFE is a tricky thing to see/remember/...
- MoonlightSentinel (19/20) Mar 07 2020 You can use an anonymous lambda to build the string in CTFE:
- =?iso-8859-1?Q?Robert_M._M=FCnch?= (11/41) Mar 08 2020 Nice... is the enum a so called "manifest constant" for which the
- =?iso-8859-1?Q?Robert_M._M=FCnch?= (6/33) Mar 08 2020 You can get rid of the enum und the static and it will work too.
- MoonlightSentinel (5/7) Mar 09 2020 That depends on your use case. You will need enum if you want to
- Timon Gehr (5/9) Mar 09 2020 It turns out that if you do use this standard idiom, you might end up
I want to create a "CREATE TABLE data (...)" where the columns are derived from struct member names. Something like: string s = "CREATE TABLE data("; static foreach(f; FieldNameTuple!myStruct) { s ~= f ~ ","; } s ~= ");"; Which of course doesn't work... I didn't find any reference how to build-up strings in a statif foreach loop. Is this possible at all? -- Robert M. Münch http://www.saphirion.com smarter | better | faster
Mar 07 2020
On Saturday, 7 March 2020 at 16:30:59 UTC, Robert M. Münch wrote:Which of course doesn't work... I didn't find any reference how to build-up strings in a statif foreach loop. Is this possible at all?Use regular foreach with a regular string. Put that inside a function. Then simply use that function to initialize your other thing and enjoy the magic of CTFE!
Mar 07 2020
On 2020-03-07 16:40:15 +0000, Adam D. Ruppe said:Use regular foreach with a regular string. Put that inside a function. Then simply use that function to initialize your other thing and enjoy the magic of CTFE!Perfect! This implicit CTFE is a tricky thing to see/remember/... Feeling a bit dumb but, hey it works :-) -- Robert M. Münch http://www.saphirion.com smarter | better | faster
Mar 07 2020
On Saturday, 7 March 2020 at 16:30:59 UTC, Robert M. Münch wrote:Is this possible at all?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);"
Mar 07 2020
On 2020-03-07 16:41:47 +0000, MoonlightSentinel said:On Saturday, 7 March 2020 at 16:30:59 UTC, Robert M. Münch wrote:Nice... is the enum a so called "manifest constant" for which the initializer is evaluated at compile time? OT: The pragma seems to print the string twice... at least here on my side. OT2: Looks like I have to read through the language spec again... and most likely over and over again, to have all these tricks at my finger-tips. -- Robert M. Münch http://www.saphirion.com smarter | better | fasterIs this possible at all?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);"
Mar 08 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
Mar 08 2020
On Sunday, 8 March 2020 at 20:28:01 UTC, Robert M. Münch wrote:You can get rid of the enum [...]That depends on your use case. You will need enum if you want to use the value at compile time (e.g. when using it as a template parameter). Otherwise a normal string will suffice.und the static and it will work too.Yesh, I forgot about the old implicit static foreach loop.
Mar 09 2020
On 07.03.20 17:41, MoonlightSentinel wrote:On Saturday, 7 March 2020 at 16:30:59 UTC, Robert M. Münch wrote:It turns out that if you do use this standard idiom, you might end up getting blamed for an unrelated DMD bug though: https://github.com/dlang/dmd/pull/9922 :o)Is this possible at all?You can use an anonymous lambda to build the string in CTFE:
Mar 09 2020