www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - macros: type save, or what?

reply 0ffh <spam frankhirsch.net> writes:
As to the matter of type save macros:
Using some imaginary macro sytax (as I don't know the real one),
but with all confidence that this will work with D macros.

macro inc(a)
// increment an integer
{
   static if (typeof(a).stringof=="int")
   {
     ++a;
   }
   else
   {
     // raise compile time error
     static assert(false,"inc wanna int!!11!");
   }
}

There might even be a more elegant way, but what teh heck... :)

Regards, Frank
Sep 11 2007
next sibling parent 0ffh <spam frankhirsch.net> writes:
0ffh wrote:
 There might even be a more elegant way, but what teh heck... :)

Admittedly it would be nicer to have "macro inc(lvalue int a)"... Walter? Regards, Frank
Sep 11 2007
prev sibling next sibling parent reply "Janice Caron" <caron800 googlemail.com> writes:
I don't think macros need to be typesafe in quite the same way that
variable declarations do. I see them as an offshoot of mixins, where
the type is known at the point of expansion, rather than the point of
declaration. I have no problem.

On another thread, I argued that
const T name = value;

should be the appropriate syntax for declaring constants. But I have
nothing against macros in general.
Sep 11 2007
parent reply 0ffh <spam frankhirsch.net> writes:
Janice Caron wrote:
 On another thread, I argued that
 const T name = value;
 
 should be the appropriate syntax for declaring constants. But I have
 nothing against macros in general.

I don't see why a const should be different from a literal when it comes to type checking, and a literal is what you'll effectively get, when you use "macro a=5;" or whatever. Anyways, this tread goes a bit beyond what we had in "Const sucks", that is why I gave it its own name... :) Regards, Frank
Sep 11 2007
parent 0ffh <spam frankhirsch.net> writes:
Janice Caron wrote:
 But if macro becomes the "official" way to declare constants then you
 don't get the option to specify the type, because "macro int x = 42;"
 won't compile.

I wonder if this might work? I admit it looks rather ugly, though... macro defconst(T,name,value) { macro name (cast(T)value); } void main() { defconst(int,x,5); }
 True. I /like/ macros. They can be used for complex and powerful
 abstractions. But if all you want to do is declare a constant, they're
 overkill. They're not the right tool for the job.

If you say it just doesn't feel right for you, I can understand that! I also like to make use of my freedom to be irrational... ;-) Regards, Frank
Sep 11 2007
prev sibling next sibling parent "Janice Caron" <caron800 googlemail.com> writes:
On 9/11/07, 0ffh <spam frankhirsch.net> wrote:
 I don't see why a const should be different from a literal when it comes
 to type checking, and a literal is what you'll effectively get, when you
 use "macro a=5;" or whatever.

Type-checking may be a red herring then. It just feels /right/ to me that you should use const int x = 42; to declare a constant. It feels natural. It's what instinct tells me to do. At least with const, specifying the type explicitly is optional. Both of these will compile: const x = 42; const int x = 42; But if macro becomes the "official" way to declare constants then you don't get the option to specify the type, because "macro int x = 42;" won't compile.
 Anyways, this tread goes a bit beyond what we had in "Const sucks", that
 is why I gave it its own name... :)

True. I /like/ macros. They can be used for complex and powerful abstractions. But if all you want to do is declare a constant, they're overkill. They're not the right tool for the job. (I prefer the keyword "define" to "macro" though) :-)
Sep 11 2007
prev sibling next sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"0ffh" <spam frankhirsch.net> wrote in message 
news:fc6oog$8ll$1 digitalmars.com...

   static if (typeof(a).stringof=="int")

OT: I really hope you haven't been using this idiom in your code. Just do "is(typeof(a) == int)".
Sep 11 2007
parent 0ffh <spam frankhirsch.net> writes:
Jarrett Billingsley wrote:
 OT: I really hope you haven't been using this idiom in your code.

Nah, haven't never needed nothing like that, that's why I never cared to look up how it's done properly; although I imagined that I faintly remembered there was a more elegant way, hence my comment at the end. Regards, Frank
Sep 11 2007
prev sibling parent renoX <renosky free.fr> writes:
0ffh a écrit :
 As to the matter of type save macros:

I don't know if typesafe macros will be used that much though, duck-typing (ie if it compiles then it's good) seems enough most of the time, have you a specific example where typesafe macros are useful? Regards, renoX
 Using some imaginary macro sytax (as I don't know the real one),
 but with all confidence that this will work with D macros.
 
 macro inc(a)
 // increment an integer
 {
   static if (typeof(a).stringof=="int")
   {
     ++a;
   }
   else
   {
     // raise compile time error
     static assert(false,"inc wanna int!!11!");
   }
 }
 
 There might even be a more elegant way, but what teh heck... :)
 
 Regards, Frank

Sep 12 2007