Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
February 25, 2019 Simplifying a string mixin | ||||
---|---|---|---|---|
| ||||
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? Also, what is the most proper thing to check that `name` is a proper identified (not say !@#)? |
February 25, 2019 Re: Simplifying a string mixin | ||||
---|---|---|---|---|
| ||||
Posted in reply to Victor Porton | 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. |
February 26, 2019 Re: Simplifying a string mixin | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | 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:
>> Also, what is the most proper thing to check that `name` is a proper identified (not say !@#)?
>
> just let the compiler do it imo.
Agreed. There are times when error message can be significantly more readable though, if testing for valid names is done in intelligent places. In this case, something like this can be of use:
enum isValidName(string s) = __traits(compiles, { mixin("int "~s~";"); });
unittest {
static assert(isValidName!"a");
static assert(isValidName!"a_B");
static assert(!isValidName!"6a");
static assert(!isValidName!"");
static assert(!isValidName!"!");
}
--
Simen
|
February 26, 2019 Re: Simplifying a string mixin | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | 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.
|
February 26, 2019 Re: Simplifying a string mixin | ||||
---|---|---|---|---|
| ||||
Posted in reply to Victor Porton | 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 |
Copyright © 1999-2021 by the D Language Foundation