Simplifying a string mixin

bauss jj_1337 at live.dk
Tue Feb 26 10:32:02 UTC 2019


On Monday, 25 February 2019 at 21:04:48 UTC, Adam D. Ruppe wrote:
> On Monday, 25 February 2019 at 20:57:37 UTC, Victor Porton 
> wrote:
>> Can string mixing be split into several parts?
>>
>> I have a mixin like this:
>>
>>     mixin("struct " ~ name ~ " {\n" ~
>>           "  struct Regular {\n" ~
>>           "    // ..." ~
>>           "  }\n" ~
>>           "  struct WithDefaults {\n" ~
>>           "    // ..." ~
>>           "  }\n" ~
>>           '}');
>
> I'd probably write that as:
>
> mixin(q{
>   struct } ~ name ~ q{ {
>     struct Regular {
>        // stuff
>     }
>     struct With Defaults {
>        // ...
>     }
>   }
> });
>
>> Also, what is the most proper thing to check that `name` is a 
>> proper identified (not say !@#)?
>
> just let the compiler do it imo.

Or you could do:

mixin(format(q{
   struct %s {
     struct Regular {
        // stuff
     }
     struct With Defaults {
        // ...
     }
   }
}, name));

That way it's way more readable.


More information about the Digitalmars-d-learn mailing list