Jump to page: 1 2
Thread overview
Mixin helper help
Jan 12, 2023
John Chapman
Jan 12, 2023
Hipreme
Jan 13, 2023
bauss
Jan 13, 2023
Ali Çehreli
Jan 14, 2023
bauss
Jan 14, 2023
Salih Dincer
Jan 16, 2023
Ali Çehreli
Jan 16, 2023
bauss
Jan 13, 2023
Salih Dincer
Jan 14, 2023
John Chapman
Jan 15, 2023
Adam D Ruppe
Jan 14, 2023
TheZipCreator
Jan 15, 2023
Salih Dincer
January 12, 2023

I'm obviously doing something wrong, but don't quite understand.

mixin template helper() {
  mixin("writeln(12);");
}

struct Foo {
  void opDispatch(string name)() {
    import std.stdio;
    mixin helper!();
    //mixin("writeln(12);");
  }
}

void main() {
  Foo.init.opDispatch!"bar"();
}

The compiler emits these errors about the mixin ("writeln(12);"):
unexpected ( in declarator
basic type expected, not 12
found 12 when expecting )
no identifier for declarator writeln(_error_)
semicolon expected following function declaration
declaration expected, not )

Why does the commented code work but the mixin not? Thanks for any pointers.

January 12, 2023

On Thursday, 12 January 2023 at 08:03:34 UTC, John Chapman wrote:

>

I'm obviously doing something wrong, but don't quite understand.

mixin template helper() {
  mixin("writeln(12);");
}

struct Foo {
  void opDispatch(string name)() {
    import std.stdio;
    mixin helper!();
    //mixin("writeln(12);");
  }
}

void main() {
  Foo.init.opDispatch!"bar"();
}

The compiler emits these errors about the mixin ("writeln(12);"):
unexpected ( in declarator
basic type expected, not 12
found 12 when expecting )
no identifier for declarator writeln(_error_)
semicolon expected following function declaration
declaration expected, not )

Why does the commented code work but the mixin not? Thanks for any pointers.

mixin template cannot be used like that. The only statement it accepts are declaration statements: Look at https://dlang.org/spec/module.html#MixinDeclaration

It says it must compile to a valid DeclDef, which means you can't put code like that.

Mixin templates are used only for declaring new variables, types and functions, it can't simply put call statements like that. You could do this by simply calling a function such as:

void helper()
{
    writeln(12);
}

I think you'll need to comment more on your problem if you wish specialized help

January 13, 2023

On Thursday, 12 January 2023 at 08:03:34 UTC, John Chapman wrote:

>

I'm obviously doing something wrong, but don't quite understand.

mixin template helper() {
  mixin("writeln(12);");
}

struct Foo {
  void opDispatch(string name)() {
    import std.stdio;
    mixin helper!();
    //mixin("writeln(12);");
  }
}

void main() {
  Foo.init.opDispatch!"bar"();
}

The compiler emits these errors about the mixin ("writeln(12);"):
unexpected ( in declarator
basic type expected, not 12
found 12 when expecting )
no identifier for declarator writeln(_error_)
semicolon expected following function declaration
declaration expected, not )

Why does the commented code work but the mixin not? Thanks for any pointers.

Mixin templates cannot have statements directly, so you need two changes for your code to work:

  1. Change your mixin template to something like this:
mixin template helper() {
    // we place the statements in this function instead
    void helper() {
        mixin("writeln(12);");
    }
}
  1. Change the place where you instantiate to this:
struct Foo {
  void opDispatch(string name)() {
    import std.stdio;
    mixin helper!();
    helper(); // calling the function in the mixin template
  }
}
January 13, 2023

On Thursday, 12 January 2023 at 08:03:34 UTC, John Chapman wrote:

>

Why does the commented code work but the mixin not? Thanks for any pointers.

Why not directly use the mixin template for opDispatch()?

mixin template helper() {
  void opDispatch(string name)() {
    import std.stdio;
    writeln(12);
  }
}

struct Foo {
  mixin helper;
  // ...
}

void main() {
  Foo.init.opDispatch!"bar"();
}

SDB@79

January 13, 2023
On 1/13/23 00:48, bauss wrote:

> 1. Change your mixin template to something like this:

There was a technique as a workaround for this template mixin limitation but I can't find it right now.

> 2. Change the place where you instantiate to this:

I think the workaround I am trying to remember would not require any change for the users.

Ok, it was something like this:

mixin template myStatement() {
    auto doIt() {
        import std.stdio : writeln;
        writeln("hi");
        return 0;
    }

    auto ignoreThis = doIt();
}

void main() {
    mixin myStatement!();
    mixin myStatement!();
}

Ali

January 14, 2023
On Friday, 13 January 2023 at 16:54:34 UTC, Ali Çehreli wrote:
> On 1/13/23 00:48, bauss wrote:
>
> > 1. Change your mixin template to something like this:
>
> There was a technique as a workaround for this template mixin limitation but I can't find it right now.
>
> > 2. Change the place where you instantiate to this:
>
> I think the workaround I am trying to remember would not require any change for the users.
>
> Ok, it was something like this:
>
> mixin template myStatement() {
>     auto doIt() {
>         import std.stdio : writeln;
>         writeln("hi");
>         return 0;
>     }
>
>     auto ignoreThis = doIt();
> }
>
> void main() {
>     mixin myStatement!();
>     mixin myStatement!();
> }
>
> Ali

That's a good one!
January 14, 2023

On Friday, 13 January 2023 at 14:32:44 UTC, Salih Dincer wrote:

>

Why not directly use the mixin template for opDispatch()?

My opDispatch generates code based on the arguments passed, interpolating variable names and functions based on the type. I wanted to remove the double braces in my static foreach (needed as I declared some aliases inside but since it creates a scope those new variables can't be referred to outside of it). I saw Adam's post here http://dpldocs.info/this-week-in-d/Blog.Posted_2022_12_26.html showing use of a "helper" template, and I was trying to adapt it.

I've since just dropped the double braces and removed the aliases. It's not as clean but works.

January 14, 2023
On Saturday, 14 January 2023 at 02:51:56 UTC, bauss wrote:
>>
>> Ali
>
> That's a good one!

also very very good, it's clever!

SDB@79
January 14, 2023

On Thursday, 12 January 2023 at 08:03:34 UTC, John Chapman wrote:

>

I'm obviously doing something wrong, but don't quite understand.

mixin template helper() {
  mixin("writeln(12);");
}

struct Foo {
  void opDispatch(string name)() {
    import std.stdio;
    mixin helper!();
    //mixin("writeln(12);");
  }
}

void main() {
  Foo.init.opDispatch!"bar"();
}

The compiler emits these errors about the mixin ("writeln(12);"):
unexpected ( in declarator
basic type expected, not 12
found 12 when expecting )
no identifier for declarator writeln(_error_)
semicolon expected following function declaration
declaration expected, not )

Why does the commented code work but the mixin not? Thanks for any pointers.

This is not the purpose mixin templates are meant to serve. They're for copying declarations into scopes (and as such only support declarations in them). Instead, I think what you want is

template helper() {
	const char[] helper = `writeln(12);`;
}

struct Foo {
	void opDispatch(string name)() {
		import std.stdio;
		mixin(helper!());
	}
}

void main() {
	Foo.init.opDispatch!"bar"();
}
January 15, 2023

On Saturday, 14 January 2023 at 18:57:21 UTC, John Chapman wrote:

>

I wanted to remove the double braces in my static foreach (needed as I declared some aliases inside but since it creates a scope those new variables can't be referred to outside of it).

Inside a function, you can often just use plain foreach instead of static foreach and simplify things. But it depends on the whole context.

Where the helper thing helps a lot is outside functions, where normal foreach and double brace are both prohibited.

« First   ‹ Prev
1 2