www.digitalmars.com         C & C++   DMDScript  

D - Yet another property idea

reply Russell Borogove <kaleja estarcion.com> writes:
The property .symbol produces a text string corresponding
to the identifier it's applied to:

    int foo = 3;

    printf( "%s = %d\n", foo.symbol, foo );  // print "foo = 3"

This is purely a compile time operation, generating string
representations only on demand.

This kind of thing is indispendable for debugging, and
valuable in general use as well. IMO, it offers a slight
improvement on the "Arrays that parallel an enum"
suggestion in:

  http://www.digitalmars.com/d/ctod.html#arrayenum

-RB
May 19 2002
parent reply "Walter" <walter digitalmars.com> writes:
"Russell Borogove" <kaleja estarcion.com> wrote in message
news:3CE82DF2.70608 estarcion.com...
 The property .symbol produces a text string corresponding
 to the identifier it's applied to:

     int foo = 3;

     printf( "%s = %d\n", foo.symbol, foo );  // print "foo = 3"

 This is purely a compile time operation, generating string
 representations only on demand.

I might be missing something, but what is the advantage over: printf( "%s = %d\n", "foo", foo ); // print "foo = 3"
May 19 2002
parent reply Jonathan Andrew <jon ece.arizona.edu> writes:
Walter wrote:

 "Russell Borogove" <kaleja estarcion.com> wrote in message
 news:3CE82DF2.70608 estarcion.com...
 The property .symbol produces a text string corresponding
 to the identifier it's applied to:

     int foo = 3;

     printf( "%s = %d\n", foo.symbol, foo );  // print "foo = 3"

 This is purely a compile time operation, generating string
 representations only on demand.

I might be missing something, but what is the advantage over: printf( "%s = %d\n", "foo", foo ); // print "foo = 3"

I suppose you could write a generic debug function i.e. void debug(int var) { printf("%s = %d\n", var.symbol, var); } debug(foo); There are probably a lot of other uses, I agree that it doesn't look too helpful at first, but there are probably lots of other tricks you could do with it. (Just as long as it's read only!!) -Jon
May 20 2002
parent reply "Pavel Minayev" <evilone omen.ru> writes:
"Jonathan Andrew" <jon ece.arizona.edu> wrote in message
news:3CE921D7.CE5E4334 ece.arizona.edu...

 I suppose you could write a generic debug function i.e.

 void debug(int var)
 {
   printf("%s = %d\n", var.symbol, var);
 }

 debug(foo);

Being resolved at compile time, it'd write "var = 666" (since var.symbol would probably give "var").
May 20 2002
next sibling parent reply "OddesE" <OddesE_XYZ hotmail.com> writes:
"Pavel Minayev" <evilone omen.ru> wrote in message
news:acbj3h$1263$1 digitaldaemon.com...
 "Jonathan Andrew" <jon ece.arizona.edu> wrote in message
 news:3CE921D7.CE5E4334 ece.arizona.edu...

 I suppose you could write a generic debug function i.e.

 void debug(int var)
 {
   printf("%s = %d\n", var.symbol, var);
 }

 debug(foo);

Being resolved at compile time, it'd write "var = 666" (since var.symbol would probably give "var").

class Foo { int i; } class Bar: public Foo { } Foo obj = new Bar; obj.i = 3; printf( "%s == %d\n", obj.symbol, obj.i); // prints "Bar == 3" So maybe slightly more helpfull than you might think at first... -- Stijn OddesE_XYZ hotmail.com http://OddesE.cjb.net _________________________________________________ Remove _XYZ from my address when replying by mail
May 20 2002
next sibling parent Patrick Down <pat codemoon.com> writes:
"OddesE" <OddesE_XYZ hotmail.com> wrote in news:acboqg$17gb$1
 digitaldaemon.com:

 "Pavel Minayev" <evilone omen.ru> wrote in message
 news:acbj3h$1263$1 digitaldaemon.com...
 "Jonathan Andrew" <jon ece.arizona.edu> wrote in message

Foo obj = new Bar; obj.i = 3; printf( "%s == %d\n", obj.symbol, obj.i); // prints "Bar == 3"

obj.classinfo.name
May 20 2002
prev sibling next sibling parent reply "Pavel Minayev" <evilone omen.ru> writes:
"OddesE" <OddesE_XYZ hotmail.com> wrote in message
news:acboqg$17gb$1 digitaldaemon.com...

 class Foo
 {
   int i;
 }

 class Bar: public Foo
 {
 }

 Foo obj = new Bar;
 obj.i = 3;
 printf( "%s == %d\n", obj.symbol, obj.i);

 // prints "Bar == 3"

Actually, it'd print "obj" (it's the name of the _symbol_, and not its type). For typename, use classinfo.name.
May 20 2002
parent "OddesE" <OddesE_XYZ hotmail.com> writes:
"Pavel Minayev" <evilone omen.ru> wrote in message
news:accfor$1rop$1 digitaldaemon.com...
 "OddesE" <OddesE_XYZ hotmail.com> wrote in message
 news:acboqg$17gb$1 digitaldaemon.com...

 class Foo
 {
   int i;
 }

 class Bar: public Foo
 {
 }

 Foo obj = new Bar;
 obj.i = 3;
 printf( "%s == %d\n", obj.symbol, obj.i);

 // prints "Bar == 3"

Actually, it'd print "obj" (it's the name of the _symbol_, and not its type). For typename, use classinfo.name.

You are right ofcourse! Sorry I was confused. -- Stijn OddesE_XYZ hotmail.com http://OddesE.cjb.net _________________________________________________ Remove _XYZ from my address when replying by mail
May 21 2002
prev sibling parent "Sean L. Palmer" <seanpalmer earthlink.net> writes:
So does obj.symbol give its type name, or its value's name?  Variable name
or type name?  Or perhaps both i.e. "Bar[]* foo"

Sean

"OddesE" <OddesE_XYZ hotmail.com> wrote in message
news:acboqg$17gb$1 digitaldaemon.com...
 "Pavel Minayev" <evilone omen.ru> wrote in message
 news:acbj3h$1263$1 digitaldaemon.com...
 "Jonathan Andrew" <jon ece.arizona.edu> wrote in message
 news:3CE921D7.CE5E4334 ece.arizona.edu...

 I suppose you could write a generic debug function i.e.

 void debug(int var)
 {
   printf("%s = %d\n", var.symbol, var);
 }

 debug(foo);

Being resolved at compile time, it'd write "var = 666" (since var.symbol would probably give "var").

class Foo { int i; } class Bar: public Foo { } Foo obj = new Bar; obj.i = 3; printf( "%s == %d\n", obj.symbol, obj.i); // prints "Bar == 3" So maybe slightly more helpfull than you might think at first...

May 21 2002
prev sibling parent Jonathan Andrew <jon ece.arizona.edu> writes:
Pavel Minayev wrote:

 "Jonathan Andrew" <jon ece.arizona.edu> wrote in message
 news:3CE921D7.CE5E4334 ece.arizona.edu...

 I suppose you could write a generic debug function i.e.

 void debug(int var)
 {
   printf("%s = %d\n", var.symbol, var);
 }

 debug(foo);

Being resolved at compile time, it'd write "var = 666" (since var.symbol would probably give "var").

Hmm, good point. -Jon
May 21 2002