www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - mixins: Shouldn't this work?

reply mike <vertex gmx.at> writes:
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
parent reply Kirk McDonald <kirklin.mcdonald gmail.com> writes:
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
parent reply mike <vertex gmx.at> writes:
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
parent Daniel Keep <daniel.keep+lists gmail.com> writes:
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
Jan 05 2007