mixins: Shouldn't this work?
Daniel Keep
daniel.keep+lists at gmail.com
Fri Jan 5 22:14:03 PST 2007
mike wrote:
> Am 04.01.2007, 00:27 Uhr, schrieb Kirk McDonald
> <kirklin.mcdonald at gmail.com>:
>
>> 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. :-)
>
>
> Ow! I see. So no way to do that?
>
> -mike
>
Since you can't mixin statements, not directly. Any way you do it, you
would end up using a function at some point, so you may as well just do
that, and rely on the compiler to inline the function for you.
*Actually reads code*
Ooooh, I see what you're up to. Hmm... that is a tricky one. You could
try using a templated function with a delegate parameter... I have no
idea if this actually works; perhaps a template guru could help you with
the details :P
> auto trace(T_Return)(char[] name, T_Return delegate() dg)
> {
> debug scope(failure) writefln("Trace: %s", name);
> return dg();
> }
>
> float foo(float x, float y)
> {
> return trace!(float)("foo",
> {
> return x / y;
> });
> }
NB: You might be able to remove the "!(float)" bit, I'm not sure. I
haven't really delved into the newer template stuff...
-- Daniel
More information about the Digitalmars-d
mailing list