digitalmars.D - Template mixin statements?
- NaN (4/4) Jan 18 2019 Template mixins can only mixin declarations. Was it ever
- Simen =?UTF-8?B?S2rDpnLDpXM=?= (15/18) Jan 18 2019 It's been suggested many times. Some explanation for why it's not
- Nicholas Wilson (3/6) Jan 18 2019 I plan to have a good discussion about it at DConf and see what
- H. S. Teoh (24/29) Jan 18 2019 I don't remember the rationale for this, but if you want to mixin
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
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
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
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