digitalmars.D.learn - Mixin Template: cannot mixin scope(exit)?
- 1100110 (13/13) Jan 13 2013 Ok, I wish to create a standard timing system so that I can measure
- 1100110 (10/23) Jan 13 2013 It appears that you cannot mixin *any* statement with
- Timon Gehr (2/31) Jan 14 2013 It is not a bug. Use a string mixin.
- 1100110 (2/37) Jan 14 2013 Well, dangit. Thanks!
- mist (1/19) Jan 14 2013 What is the rationale behind this limitation?
- 1100110 (3/19) Jan 14 2013 I too would like to know the reason behind the limitation...
- Jacob Carlborg (5/6) Jan 14 2013 I'm not sure but it might have something to do with template mixins
- Nicolas Sicard (10/34) Jan 15 2013 Mixin templates are supposed to introduce *declarations* not
- mist (7/16) Jan 15 2013 I thought template itself should compile but its statement-like
- Nicolas Sicard (4/10) Jan 15 2013 The template shouldn't compile: the D grammar says that mixin
Ok, I wish to create a standard timing system so that I can measure ~how long each function takes to execute. I wish to be able to place at the start of a function version(Time) mixin TimeExecution("funcName"); mixin template TimeExecution(T) if(isSomeString!T) { import std.stdio, std.datetime, std.conv; auto sw = StopWatch(AutoStart.yes); // Error: Declaration expected, not '(' scope(exit) writeln(T, ": ", to!Duration(sw.peek)); } Why do I receive the Error when the scope statement is included? Is this an error, or what is the rationale behind the decision? Thank you.
Jan 13 2013
On 01/13/2013 11:35 PM, 1100110 wrote:Ok, I wish to create a standard timing system so that I can measure ~how long each function takes to execute. I wish to be able to place at the start of a function version(Time) mixin TimeExecution("funcName"); mixin template TimeExecution(T) if(isSomeString!T) { import std.stdio, std.datetime, std.conv; auto sw = StopWatch(AutoStart.yes); // Error: Declaration expected, not '(' scope(exit) writeln(T, ": ", to!Duration(sw.peek)); } Why do I receive the Error when the scope statement is included? Is this an error, or what is the rationale behind the decision? Thank you.It appears that you cannot mixin *any* statement with scope([exit,success,etc]) in it. I have been rereading my copy of TDPL, and it states that mixin statements must be valid D code, and there can be multiple 'scope()' statements. Since "scope(exit) writeln();" is valid D code, and refuses to compile in a mixin, I assume that this is a bug. I've been digging through the bug tracker and I cannot find a duplicate bug, so if someone can confirm that this is a bug, I'll create a report.
Jan 13 2013
On 01/14/2013 07:26 AM, 1100110 wrote:On 01/13/2013 11:35 PM, 1100110 wrote:It is not a bug. Use a string mixin.Ok, I wish to create a standard timing system so that I can measure ~how long each function takes to execute. I wish to be able to place at the start of a function version(Time) mixin TimeExecution("funcName"); mixin template TimeExecution(T) if(isSomeString!T) { import std.stdio, std.datetime, std.conv; auto sw = StopWatch(AutoStart.yes); // Error: Declaration expected, not '(' scope(exit) writeln(T, ": ", to!Duration(sw.peek)); } Why do I receive the Error when the scope statement is included? Is this an error, or what is the rationale behind the decision? Thank you.It appears that you cannot mixin *any* statement with scope([exit,success,etc]) in it. I have been rereading my copy of TDPL, and it states that mixin statements must be valid D code, and there can be multiple 'scope()' statements. Since "scope(exit) writeln();" is valid D code, and refuses to compile in a mixin, I assume that this is a bug. I've been digging through the bug tracker and I cannot find a duplicate bug, so if someone can confirm that this is a bug, I'll create a report.
Jan 14 2013
On 01/14/2013 02:03 AM, Timon Gehr wrote:On 01/14/2013 07:26 AM, 1100110 wrote:Well, dangit. Thanks!On 01/13/2013 11:35 PM, 1100110 wrote:It is not a bug. Use a string mixin.Ok, I wish to create a standard timing system so that I can measure ~how long each function takes to execute. I wish to be able to place at the start of a function version(Time) mixin TimeExecution("funcName"); mixin template TimeExecution(T) if(isSomeString!T) { import std.stdio, std.datetime, std.conv; auto sw = StopWatch(AutoStart.yes); // Error: Declaration expected, not '(' scope(exit) writeln(T, ": ", to!Duration(sw.peek)); } Why do I receive the Error when the scope statement is included? Is this an error, or what is the rationale behind the decision? Thank you.It appears that you cannot mixin *any* statement with scope([exit,success,etc]) in it. I have been rereading my copy of TDPL, and it states that mixin statements must be valid D code, and there can be multiple 'scope()' statements. Since "scope(exit) writeln();" is valid D code, and refuses to compile in a mixin, I assume that this is a bug. I've been digging through the bug tracker and I cannot find a duplicate bug, so if someone can confirm that this is a bug, I'll create a report.
Jan 14 2013
What is the rationale behind this limitation?It appears that you cannot mixin *any* statement with scope([exit,success,etc]) in it. I have been rereading my copy of TDPL, and it states that mixin statements must be valid D code, and there can be multiple 'scope()' statements. Since "scope(exit) writeln();" is valid D code, and refuses to compile in a mixin, I assume that this is a bug. I've been digging through the bug tracker and I cannot find a duplicate bug, so if someone can confirm that this is a bug, I'll create a report.It is not a bug. Use a string mixin.
Jan 14 2013
On 01/14/2013 04:44 AM, mist wrote:I too would like to know the reason behind the limitation... My Code was nice and pretty incorrect.What is the rationale behind this limitation?It appears that you cannot mixin *any* statement with scope([exit,success,etc]) in it. I have been rereading my copy of TDPL, and it states that mixin statements must be valid D code, and there can be multiple 'scope()' statements. Since "scope(exit) writeln();" is valid D code, and refuses to compile in a mixin, I assume that this is a bug. I've been digging through the bug tracker and I cannot find a duplicate bug, so if someone can confirm that this is a bug, I'll create a report.It is not a bug. Use a string mixin.
Jan 14 2013
On 2013-01-14 11:44, mist wrote:What is the rationale behind this limitation?I'm not sure but it might have something to do with template mixins introduce a new scope or similar. -- /Jacob Carlborg
Jan 14 2013
On Monday, 14 January 2013 at 06:26:33 UTC, 1100110 wrote:On 01/13/2013 11:35 PM, 1100110 wrote:Mixin templates are supposed to introduce *declarations* not statements. Eg. even this shouldn't compile, should it? --- mixin template TimeExecution(T) if(isSomeString!T) { import std.stdio; writeln(T); // statement } ---Ok, I wish to create a standard timing system so that I can measure ~how long each function takes to execute. I wish to be able to place at the start of a function version(Time) mixin TimeExecution("funcName"); mixin template TimeExecution(T) if(isSomeString!T) { import std.stdio, std.datetime, std.conv; auto sw = StopWatch(AutoStart.yes); // Error: Declaration expected, not '(' scope(exit) writeln(T, ": ", to!Duration(sw.peek)); } Why do I receive the Error when the scope statement is included? Is this an error, or what is the rationale behind the decision? Thank you.It appears that you cannot mixin *any* statement with scope([exit,success,etc]) in it.
Jan 15 2013
I thought template itself should compile but its statement-like instantiation should not. By the way, if all you want is to split out some generic statement block without using dirty string mixins, template functions with alias parameters may do. I.e. http://dpaste.1azy.net/68ad8133 Don't know what about inlining for it though.Mixin templates are supposed to introduce *declarations* not statements. Eg. even this shouldn't compile, should it? --- mixin template TimeExecution(T) if(isSomeString!T) { import std.stdio; writeln(T); // statement } ---
Jan 15 2013
On Tuesday, 15 January 2013 at 11:19:50 UTC, mist wrote:I thought template itself should compile but its statement-like instantiation should not.The template shouldn't compile: the D grammar says that mixin templates inject declarations only. Hence the text of the error.By the way, if all you want is to split out some generic statement block without using dirty string mixins, template functions with alias parameters may do. I.e. http://dpaste.1azy.net/68ad8133Yes, but only a string mixin can inject a scope statement.
Jan 15 2013