Simplifying a string mixin

Jacob Carlborg doob at me.com
Tue Feb 26 10:40:07 UTC 2019


On 2019-02-25 21:57, 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 would like to split it into several mixins (preferably using regular 
> non-string mixins).
> 
> Are things like this possible?

You can create multiple functions that returns a portion of the string 
to mixin. For example:

mixin(generateCode(name));

string generateCode(string name)
{
     return part1(name) ~ part2() ~ part3(); // and so on
}

> Also, what is the most proper thing to check that `name` is a proper 
> identified (not say !@#)?

Have one of the above functions to do the validation.

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list