Why does this mixin fail to compile?
ryuukk_
ryuukk.dev at gmail.com
Sat Jul 6 08:05:39 UTC 2024
On Wednesday, 3 July 2024 at 03:52:41 UTC, Steven Schveighoffer
wrote:
> On Tuesday, 2 July 2024 at 07:23:42 UTC, ryuukk_ wrote:
>
>> I said it 2 times already, i don't want string concatenation,
>> i'll benchmark later, but not right now, right now i'm looking
>> for a functioning code without string concatenation
>
> Your buffer solution works, but you need to put it inside a
> *function*, not at declaration scope. What you wrote declares
> *runtime* variables, which wouldn't be usable at compile time
> (if you got it past the parser, which is where it was failing).
>
> So for instance:
>
> ```d
> mixin template implement()
> {
> mixin(() {
> // ctfe new array is basically the same as static array
> char[] buffer = new char[4096];
> int pos = 0;
>
> void append(string str)
> {
> buffer[pos .. pos + str.length] = str[];
> pos += str.length;
> }
>
> append("void* ctx;");
> return buffer[0 .. pos];
> }());
> }
> ```
>
> And yes, it is faster to do this than appending. But you have
> to do a *lot* of it to make a huge difference.
>
> -Steve
That's annoying that it couldn't have been more straight
forward.. but that'll do it for now, it works, thanks
More information about the Digitalmars-d-learn
mailing list