How to insert code in place with templates/mixins?
bachmeier
no at spam.net
Mon Dec 20 18:58:39 UTC 2021
On Monday, 20 December 2021 at 18:23:44 UTC, rempas wrote:
> On Monday, 20 December 2021 at 18:12:35 UTC, Stanislav Blinov
> wrote:
>>
>> https://dlang.org/spec/traits.html#identifier
>
> Thanks!!! Finally I was able to do it! The code is the
> following (well not in my final project but it's a
> demonstration):
>
> ```
> enum add_char(string c) =
> `if (stdout_index < STDOUT_BUF_LEN) {
> stdout_buffer[stdout_index++] =` ~ c ~ `;
> continue;
> } else {
> sys_write(1, stdout_buffer.ptr, cast(i32)stdout_index);
> stdout_index = 0;
> stdout_buffer[stdout_index++] =` ~ c ~ `;
> continue;
> }`;
>
> void main() {
> mixin(add_char!(__traits(identifier, c)));
> }
> ```
>
> I don't know if there is another way to do it but this works
> for me. Also another thing that I want to ask is if the "mixin"
> is generated every time inside a loop and if there is a better
> way to do that?
You can see the ["String mixins" section
here](http://ddili.org/ders/d.en/mixin.html) for more details.
Mixins are generated at compile time, so if you're referring to a
string mixin inside a runtime loop, the code will not be
generated every time the loop runs.
More information about the Digitalmars-d-learn
mailing list