Thread overview
Template mixin statements?
Jan 18, 2019
NaN
Jan 18, 2019
Simen Kjærås
Jan 18, 2019
Nicholas Wilson
Jan 18, 2019
H. S. Teoh
January 18, 2019
Template mixins can only mixin declarations. Was it ever considered to allow them to mixin statements? Is it ever likely to be added? Or are there reasons it wasnt allowed?

Thanks.
January 18, 2019
On Friday, 18 January 2019 at 11:07:10 UTC, NaN wrote:
> Template mixins can only mixin declarations. Was it ever considered to allow them to mixin statements? Is it ever likely to be added? Or are there reasons it wasnt allowed?

It's been suggested many times. Some explanation for why it's not allowed is in https://issues.dlang.org/show_bug.cgi?id=1734, but the motivations for disallowing it aren't really clear there.

Anyways, there is of course a workaround:

mixin template tmp() {
    auto __tmp = (() => i = 3)();
}

unittest {
    int i = 0;
    mixin tmp!();
    assert(i == 3);
}

--
  Simen
January 18, 2019
On Friday, 18 January 2019 at 11:07:10 UTC, NaN wrote:
> Template mixins can only mixin declarations. Was it ever considered to allow them to mixin statements? Is it ever likely to be added?

I plan to have a good discussion about it at DConf and see what can be done about/with it.

January 18, 2019
On Fri, Jan 18, 2019 at 11:07:10AM +0000, NaN via Digitalmars-d wrote:
> Template mixins can only mixin declarations. Was it ever considered to allow them to mixin statements? Is it ever likely to be added? Or are there reasons it wasnt allowed?
> 
> Thanks.

I don't remember the rationale for this, but if you want to mixin statements, you can use the Do template in the following workaround:

	import std.stdio;
	mixin template Do(alias fun) {
		int _dummy = { fun(); return 0; }();
	}

	mixin template MyMixin() {
		string strDecl = "abc";
		mixin Do!({ writeln("look ma, a statement!"); });
		int intDecl = 123;
		mixin Do!({ writeln("look ma, another statement!"); });
	}

	void main() {
		mixin MyMixin!();
		writeln(strDecl);
		writeln(intDecl);
	}

It's a bit ugly, but it works.


T

-- 
Bomb technician: If I'm running, try to keep up.