Thread overview | ||||||
---|---|---|---|---|---|---|
|
January 03, 2007 mixins: Shouldn't this work? | ||||
---|---|---|---|---|
| ||||
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
--
Erstellt mit Operas revolutionärem E-Mail-Modul: http://www.opera.com/mail/
|
January 03, 2007 Re: mixins: Shouldn't this work? | ||||
---|---|---|---|---|
| ||||
Posted in reply to mike | 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 |
January 03, 2007 Re: mixins: Shouldn't this work? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kirk McDonald | Am 04.01.2007, 00:27 Uhr, schrieb Kirk McDonald <kirklin.mcdonald@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 -- Erstellt mit Operas revolutionärem E-Mail-Modul: http://www.opera.com/mail/ |
January 06, 2007 Re: mixins: Shouldn't this work? | ||||
---|---|---|---|---|
| ||||
Posted in reply to mike | mike wrote:
> Am 04.01.2007, 00:27 Uhr, schrieb Kirk McDonald <kirklin.mcdonald@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
|
Copyright © 1999-2021 by the D Language Foundation