How to insert code in place with templates/mixins?

rempas rempas at tutanota.com
Mon Dec 20 18:23:44 UTC 2021


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?


More information about the Digitalmars-d-learn mailing list