Jump to page: 1 2 3
Thread overview
Why does this mixin fail to compile?
Jul 01
ryuukk_
Jul 01
Dennis
Jul 01
ryuukk_
Jul 01
drug007
Jul 01
ryuukk_
Jul 01
drug007
Jul 01
ryuukk_
Jul 01
drug007
Jul 01
ryuukk_
Jul 01
monkyyy
Jul 01
Dennis
Jul 02
ryuukk_
Aug 12
IchorDev
Aug 12
IchorDev
Aug 13
IchorDev
Jul 06
ryuukk_
Aug 14
cc
July 01

This simple mixin fails to compile, anyone know why?

mixin implement;

mixin template implement() {
    mixin("struct _gen(T) {");
    mixin("}");
}

void main(){}
onlineapp.d-mixin-5(5): Error: `}` expected following members in `struct` declaration
onlineapp.d-mixin-5(5):        struct `_gen` starts here
onlineapp.d(6): Error: mixin `__anonymous` incomplete mixin declaration `}`
onlineapp.d(2): Error: mixin `onlineapp.implement!()` error instantiating
July 01

On Monday, 1 July 2024 at 09:25:39 UTC, ryuukk_ wrote:

>

This simple mixin fails to compile, anyone know why?

mixin implement;

mixin template implement() {
    mixin("struct _gen(T) {");
    mixin("}");
}

A string mixin must form a complete declaration / statement / expression / type, so you can't end on an open brace.

July 01

On Monday, 1 July 2024 at 09:29:50 UTC, Dennis wrote:

>

On Monday, 1 July 2024 at 09:25:39 UTC, ryuukk_ wrote:

>

This simple mixin fails to compile, anyone know why?

mixin implement;

mixin template implement() {
    mixin("struct _gen(T) {");
    mixin("}");
}

A string mixin must form a complete declaration / statement / expression / type, so you can't end on an open brace.

this is sad, how i am supposed to do a foreach inbetween?

mixin implement!TEST;

mixin template implement(stuff) {
    mixin("struct _gen(T) {");

    // pseudo code
    static foreach(args)
        mixin("stuff");

    mixin("}");
}

void main(){}
July 01
On 01.07.2024 12:39, ryuukk_ wrote:
> On Monday, 1 July 2024 at 09:29:50 UTC, Dennis wrote:
>> On Monday, 1 July 2024 at 09:25:39 UTC, ryuukk_ wrote:
>>> This simple mixin fails to compile, anyone know why?
>>>
>>> ```D
>>> mixin implement;
>>>
>>> mixin template implement() {
>>>     mixin("struct _gen(T) {");
>>>     mixin("}");
>>> }
>>
>> A string mixin must form a complete declaration / statement / expression / type, so you can't end on an open brace.
> 
> this is sad, how i am supposed to do a foreach inbetween?
> 
> ```D
> mixin implement!TEST;
> 
> mixin template implement(stuff) {
>      mixin("struct _gen(T) {");
> 
>      // pseudo code
>      static foreach(args)
>          mixin("stuff");
> 
>      mixin("}");
> }
> 
> void main(){}
> ```

No problem, make a full string then mixin it.
July 01
On Monday, 1 July 2024 at 10:20:25 UTC, drug007 wrote:
> On 01.07.2024 12:39, ryuukk_ wrote:
>> On Monday, 1 July 2024 at 09:29:50 UTC, Dennis wrote:
>>> On Monday, 1 July 2024 at 09:25:39 UTC, ryuukk_ wrote:
>>>> This simple mixin fails to compile, anyone know why?
>>>>
>>>> ```D
>>>> mixin implement;
>>>>
>>>> mixin template implement() {
>>>>     mixin("struct _gen(T) {");
>>>>     mixin("}");
>>>> }
>>>
>>> A string mixin must form a complete declaration / statement / expression / type, so you can't end on an open brace.
>> 
>> this is sad, how i am supposed to do a foreach inbetween?
>> 
>> ```D
>> mixin implement!TEST;
>> 
>> mixin template implement(stuff) {
>>      mixin("struct _gen(T) {");
>> 
>>      // pseudo code
>>      static foreach(args)
>>          mixin("stuff");
>> 
>>      mixin("}");
>> }
>> 
>> void main(){}
>> ```
>
> No problem, make a full string then mixin it.

Your "no problem" = lot of string concatenation therefore slower and memory hungry, i have no desire to do that
July 01
On 01.07.2024 13:31, ryuukk_ wrote:
> On Monday, 1 July 2024 at 10:20:25 UTC, drug007 wrote:
>>
>> No problem, make a full string then mixin it.
> 
> Your "no problem" = lot of string concatenation therefore slower and memory hungry, i have no desire to do that

Do you think that string concatenation is the most heavy operation on using string mixin?

When you pass the mixin string to the compiler it allocates memory too. If you want to use incomplete mixin strings the compiler will accumulate it (allocating additional memory) until it builds a complete AST node to mix in at once. At best, it will cost the same as building a full string as I suggested but in general it may cost even more. Also, compilation would take longer because the compiler would need to have additional logic to detect the end of your string mixin etc. Your profit from avoiding string concatenation would almost negative.
July 01

Ok, i'll just do it and benchmark at the end

Another question:

Why doesn't this work?:

mixin implement;

mixin template implement()
{
    char[4096] buffer = 0;
    int pos = 0;

    void append(string str)
    {
        buffer[pos .. pos + str.length] = str[];
        pos += str.length;
    }

    append("void* ctx;");
    mixin(cast(string) buffer[0 .. pos]);
}
onlineapp.d(13): Error: unexpected `(` in declarator
onlineapp.d(13): Error: basic type expected, not `"void* ctx;"`
onlineapp.d(13): Error: found `"void* ctx;"` when expecting `)`
onlineapp.d(13): Error: no identifier for declarator `append(_error_)`
onlineapp.d(13): Error: semicolon expected following function declaration, not `)`
onlineapp.d(13): Error: declaration expected, not `)`
July 01
On 01.07.2024 15:26, ryuukk_ wrote:
> Ok, i'll just do it and benchmark at the end
> 
> Another question:
> 
> 
> Why doesn't this work?:
> 

I'm pretty sure your code does not work the way you think it does. String Mixins and Template Mixins are two different things. If you want to use String Mixins just make a function that creates a complete string with the source code and then mix it in. No need for Template Mixins in this case.

```D
string generateSourceCode(T)()
{
     return T.stringof ~ "* ctx;";
}

void main()
{
    mixin(generateSourceCode!int);

    static assert(is(typeof(ctx) == int*));

    assert(ctx is null);
}
```
July 01
On Monday, 1 July 2024 at 12:43:01 UTC, drug007 wrote:
> On 01.07.2024 15:26, ryuukk_ wrote:
>> Ok, i'll just do it and benchmark at the end
>> 
>> Another question:
>> 
>> 
>> Why doesn't this work?:
>> 
>
> I'm pretty sure your code does not work the way you think it does. String Mixins and Template Mixins are two different things. If you want to use String Mixins just make a function that creates a complete string with the source code and then mix it in. No need for Template Mixins in this case.
>
> ```D
> string generateSourceCode(T)()
> {
>      return T.stringof ~ "* ctx;";
> }
>
> void main()
> {
>     mixin(generateSourceCode!int);
>
>     static assert(is(typeof(ctx) == int*));
>
>     assert(ctx is null);
> }
> ```

please stick to what i wrote, i don't want string concatenation, i provide a reduced example from my project, everything should be a single template block, no extra functions other than the append() one
July 01
On Monday, 1 July 2024 at 13:00:55 UTC, ryuukk_ wrote:
> i don't want string concatenation

This limitation is very intentional, add it to the pile like file io in ctfe of stuff that the core devs think "you shouldnt even want that" for "safety"
« First   ‹ Prev
1 2 3