Const sucks

Janice Caron caron800 at googlemail.com
Tue Sep 11 10:45:41 PDT 2007


> Hmmmm... if I got that right, macros will definitely *not* be "pieces
> of text", but abstract syntax trees.

Macros have their place. I'm not knocking them. But they're not the
right mechanism for declaring constants. On the other hand,

const Type name = value;
is.

(replace const with static if necessary - see earlier in thread).

Of course, it really should be
invariant Type name = value;

with the current keywords, though if we switch from invariant/const to
const/readonly that mistake will likely disappear.


> So, if you'd do "macro x=5;" (or whatever the macro syntax will be),
> and you try `char[] s="abc"~x;` you'll get a nice, clean type error.

Looks the same as C to me.

#define x 5
strcat(s,"abc");
strcat(s,x); /* compile error */

That doesn't count as strong typing.



More information about the Digitalmars-d mailing list