digitalmars.D - mixins: Shouldn't this work?
- mike (41/41) Jan 03 2007 Hi!
- Kirk McDonald (20/66) Jan 03 2007 Mixins are for mixing-in declarations, not statements. What you're doing...
- mike (9/24) Jan 03 2007 ng =
- Daniel Keep (12/52) Jan 05 2007 Since you can't mixin statements, not directly. Any way you do it, you
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=E4rem E-Mail-Modul: http://www.opera.com/=
mail/
Jan 03 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
Jan 03 2007
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 doi=ng =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=E4rem E-Mail-Modul: http://www.opera.com/= mail/
Jan 03 2007
mike wrote:Am 04.01.2007, 00:27 Uhr, schrieb Kirk McDonald <kirklin.mcdonald gmail.com>: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 :PMixins 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? -mikeauto 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
Jan 05 2007








Daniel Keep <daniel.keep+lists gmail.com>