Comment #0 by bearophile_hugs — 2010-07-28T15:30:47Z
This is a valid D2 code (dmd 2.047):
import std.stdio;
void main() {
string a();
}
Allowing/keeping ugly and error-prone function literals in D2 is bad. It's better for D2 to keep/allow only _one_ standard, clean and readable syntax for functions pointers (and one for delegates, if necessary). Keeping C function pointer syntax or something similar in D2 causes troubles.
Comment #1 by Justin.SpahrSummers — 2010-07-28T16:17:15Z
(In reply to comment #0)
> Allowing/keeping ugly and error-prone function literals in D2 is bad. It's
> better for D2 to keep/allow only _one_ standard, clean and readable syntax for
> functions pointers (and one for delegates, if necessary). Keeping C function
> pointer syntax or something similar in D2 causes troubles.
'a' is not a function literal... it's a declaration of a nested function. The C syntax for a function pointer would be:
string (*a)();
which I think is pretty unambiguous.
Comment #2 by smjg — 2010-07-28T16:40:28Z
(In reply to comment #1)
> string (*a)();
>
> which I think is pretty unambiguous.
It isn't. Could be a function called string, which is being called with argument (*a), and the function returned from that call then called with an empty argument list.
D grammar aims to be unambiguous. Removing C-style casts has already been a step in the right direction. Removing C-style function pointer declarations would be another.
Comment #3 by dfj1esp02 — 2010-07-29T09:21:49Z
Or a static opCall on the string type.
Or an extension method opCall(string b, int a){...}
Comment #4 by smjg — 2010-07-29T14:51:27Z
But as far as the parser is concerned, that's still a function call. It's the semantic analyser that identifies whether string is an actual function, a function pointer or delegate, a type on which a static opCall is defined, an object of a type that has an opCall, or whatever.
A function pointer declaration, on the other hand, is a syntactical form distinct from all of these.
Comment #5 by bearophile_hugs — 2010-08-03T18:13:23Z
See also enhancement request 4580
Comment #6 by yebblies — 2011-06-15T07:59:44Z
In the original comment, there is no 'ugly and error-prone function literal'.
There is a nested function declaration with no body.
import std.stdio;
void main() {
string a();
pragma(msg, typeof(&a));
}
Prints:
string delegate()