mixins: Shouldn't this work?

Kirk McDonald kirklin.mcdonald at gmail.com
Wed Jan 3 15:27:29 PST 2007


mike wrote:
> Hi!
> 
> I've got this idea:
> 
> ' import std.stdio;
> '
> ' debug
> ' {
> '     void trace(char[] T)()
> '     {
> '         scope (failure) writefln("Trace: ", T);
> '     }
> ' }
> ' else
> ' {
> '     void trace(char[] T)()
> '     {
> '     }
> ' }
> '
> ' float foo(float x, float y)
> ' {
> '     mixin trace!("foo");
> '     return x / y;
> ' }
> '
> ' void bar()
> ' {
> '     writefln("result: ", foo(0., 0.));
> ' }
> 
> As far as I understand it, the "scope (failure) ..." would be mixed 
> into  foo's scope, so as soon as foo throws, the trace message should 
> be  displayed.
> 
> ' mixin trace!("msg");
> 
> is much nicer than
> 
> ' debug scope (failure) writefln("msg");
> 
> Oh, and congrats on 1.0! :)
> Hope D gets picked up by lots of people now!
> 
> -mike
> 

Mixins are for mixing-in declarations, not statements. What you're doing 
is this:

float foo(float x, float y) {
     mixin trace!("foo");
     return x / y;
}

Becomes:

float foo(float x, float y) {
     void trace() {
         scope (failure) writefln("Trace: ", "foo");
     }
     return x / y;
}

See? The "scope (failure)" is inside a nested function. It applies to 
the scope of that function, and doesn't help one iota. :-)

-- 
Kirk McDonald
Pyd: Wrapping Python with D
http://pyd.dsource.org



More information about the Digitalmars-d mailing list