www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Template mixin statements?

reply NaN <divide by.zero> writes:
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.
Jan 18 2019
next sibling parent Simen =?UTF-8?B?S2rDpnLDpXM=?= <simen.kjaras gmail.com> writes:
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
Jan 18 2019
prev sibling next sibling parent Nicholas Wilson <iamthewilsonator hotmail.com> writes:
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.
Jan 18 2019
prev sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
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.
Jan 18 2019