17 ms·
Yet another example of why C type declaration syntax could be better. First, C should have followed this golden rule: <scope> <type> <name>; // variable d
by loup-vaillant 15y ago
Yet another example of why C type declaration syntax could be better. First, C should have followed this golden rule:
<scope> <type> <name>; // variable declaration
typedef <name> = <type> // typedef declaration
(with <scope> = "static", "auto", or nothing)
That would be a first step. A second one would be to use parentheses to denote grouping. Such that extraneous parentheses does not screw up the whole type declaration:
// the following two lines are equivalent
[]*int p; // array of pointers (but this is not clear)
[](*int) p; // Ah, now this is more obvious.
int*[] p; // alternate syntax, with postfix notation
(int*)[] p; // (personally, I prefer the prefix one)
*int[] p; // mixing postfix and prefix does no good.
Same rule for const:
const *int i; // constant pointer to mutable int.
*const int i; // mutable pointer to a constant int.
*(const int) i; // again, we can clarify.
const (*int) i;
(By the way, I think we should make const the default, and use a "mutable" keyword instead. But that's another fight.)
Functions could be declared in simpler ways:
bool (int i, float x) f;
(int i, float x) bool f; // alternate syntax
(int i, float x) -> bool f; // alternate syntax 2
bool <- (int i, float x) f; // alternate syntax 3
C doesn't do currying by default, so I don't really care wether the return type goes before or after the arguments. But the name of the function should definitely be on the right of its own type, so we still follow the golden rule. Function definition would be equally simple:
bool (int i, float x) f =
{
return 2 * i + x;
}
Note the similarity with
int i = 42;
Now you want a pointer to a function? Easy: you just prefix (or postfix, depending on your ultimate choice) the type with a star:
*(bool (int i, float x)) fp = f;
// Note: this one is ambiguous without precedence rules:
*bool (int i, float x) fp = f;
// And this one clearly denotes a function wich returns
// a pointer to bool
(*bool) (int i, float x) fp = f;
Structure (and class) declaration could also use a bit of makup:
typedef Foo = struct {
int i;
float x;
bool b;
};
There, the syntax of types is much nicer, and easier to parse (it doesn't really matter for C, but C++ could really use some love).
Also worthy of mention is, this syntax above is less restricted than the ANSI one. However, the semantic restrictions still hold. This declaration for instance would be syntactically valid, but semantically bogus:
void (int i, struct { float f; bool b; }) f;
It could work if C were ducked typed. :-)
- eru 15y agoYou can probably hack together a nice language around C. Sort of like CoffeeScript around JavaScript. While you are at it, might as well throw the option of significant indentation into it---like in Haskell, but not mandantory like in Python.
- groovy2shoes 15y ago> You can probably hack together a nice language around C. That's been done at least twice already -- for some (very debatable) values of 'nice'. See C++ and Objective-C.
- eru 15y agoOh, but they tried to introduce lots of new stuff. Just stick to giving a nicer syntax.
- gsg 15y agovoid (int i, struct { float f; bool b; }) f; Apart from f being in the wrong place (and bool not being defined without including stdbool.h), this is a valid C declaration.