www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Mixin Template: cannot mixin scope(exit)?

reply 1100110 <0b1100110 gmail.com> writes:
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
parent reply 1100110 <0b1100110 gmail.com> writes:
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
next sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 01/14/2013 07:26 AM, 1100110 wrote:
 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.
It is not a bug. Use a string mixin.
Jan 14 2013
next sibling parent 1100110 <0b1100110 gmail.com> writes:
On 01/14/2013 02:03 AM, Timon Gehr wrote:
 On 01/14/2013 07:26 AM, 1100110 wrote:
 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.
It is not a bug. Use a string mixin.
Well, dangit. Thanks!
Jan 14 2013
prev sibling parent reply "mist" <none none.none> writes:
 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.
What is the rationale behind this limitation?
Jan 14 2013
next sibling parent 1100110 <0b1100110 gmail.com> writes:
On 01/14/2013 04:44 AM, mist wrote:
 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.
What is the rationale behind this limitation?
I too would like to know the reason behind the limitation... My Code was nice and pretty incorrect.
Jan 14 2013
prev sibling parent Jacob Carlborg <doob me.com> writes:
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
prev sibling parent reply "Nicolas Sicard" <dransic gmail.com> writes:
On Monday, 14 January 2013 at 06:26:33 UTC, 1100110 wrote:
 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.
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
parent reply "mist" <none none.none> writes:
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
parent "Nicolas Sicard" <dransic gmail.com> writes:
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/68ad8133
Yes, but only a string mixin can inject a scope statement.
Jan 15 2013