On 04/10/2010 10:07, Don wrote:
A great example of how C syntax is hurting us.
---
I found this bit of code in std.container, inside BinaryHeap:

size_t insert(ElementType!Store value)
{
static if (is(_store.insertBack(value)))
{
...
}
else ...

What does the static if do? It's *intended* to check if _store has a
member function insertBack(), which accepts type of a 'value'.

But instead, it ALWAYS silently does the 'else' clause.
Unless _store.insertBack is a valid *type*, (eg, alias int insertBack;).
In which case it gives an error "declaration value is already defined".

Why?

This happens because
x(y); is valid C syntax for declaring a type 'y', such that &y is of
type 'x function()'.

The C syntax is unspeakably ridiculous, useless, and downright
dangerous. It shouldn't compile.


Whoa. I considered myself completely knowledgeable of the C language, but I had no idea about this syntax. (Note: by "completely knowledgeable" I don't mean I could recite the spec by memory, but rather that at least I knew what features, syntax and semantics ANSI C 89 had available.)

Hum, your description of what "x(y);" means seems slightly incorrect though. Both in C and D, if x is a type, then it is the same as "x y;", that is, it declares a variable y with type x. If x is not a type but y is, it seems to be the same as "void x(y);", that is, it declares a function prototype named x. If both are not types, then it declares that strange thing I don't quite understand nor am I interested to...

I do vaguely recall learning about the first scenario, where a variable is declared, but I had not idea about the others. Is this mentioned in K&R TCPL 2nd edition?

Not that it matters, it's still horrid! I'm glad we're nuking it from D.

--
Bruno Medeiros - Software Engineer

Reply via email to