The “Clockwise/Spiral Rule”
c-faq.com
c-faq.com
That means most of the decisions Dennis Ritchie (with the help of others) took when designing the language were the right ones for solving the problem at hand [1], otherwise the language wouldn't have survived as strongly as it did. [2]
[1] http://cm.bell-labs.com/who/dmr/chist.pdf
[2] http://www.tiobe.com/index.php/content/paperinfo/tpci/index....
Go, being very C-like in many syntactic aspects, reworks this, and does so for a reason.
void (*signal(int, void (*fp)(int)))(int);
function<function<void(int)> (int, function<void(int)>) > signal;
It is more verbose, but easier to read, in my opinion. The return value of signal, function<void(int)>, is in one place, instead of being split into the beginning and end of the expression.As an added bonus, it can also handle lambda functions or bound methods, which the function pointer version cannot.
template<class F>
struct func_ {};
template<class Result, class... Args>
struct func_<Result(Args...)> {
typedef Result(*type)(Args...);
};
template<class F>
using func = typename func_<F>::type;
func<func<void(int)>(int, func<void(int)>)> signal; template<class T>
using ptr = T*;
ptr<ptr<void(int)>(int, ptr<void(int)>)> signal;
:)[0] http://stackoverflow.com/questions/16260417/the-spiral-rule-...
The sad truth is that it leaves mostly the embedded world, where you are stuck with C, you have to deal with GCC, you have to deal with cross compilation GCC and you have no real debugging facility. The place where you would want to avoid bugs, and the place where finding a bug is a pain and undefined behavior lurks at every street corner.
[0] http://en.cppreference.com/w/cpp/language/operator_precedenc...
Now, the bigger issue is why is the C syntax surviving. I blame Java and now Javascript. This will only get worse as more people get to know it. At least javascript doesn't have so many type shenanigans: try parsing in your head a pointer to a C function that takes a pointer to a struct or something.
Still, C is a pretty nice fallback when you have nailed your algorithm and must absolutely get the last drop of performance (and want to still reason about what the compiler is doing, so C++ is out). Or when you want to interface with anything written in C.
() Then again, he could just have done a Lisp and removed almost all syntax.
So, couldn't you avoid much of the confusion by mandating that function pointers may refer to other function pointers only through typedefs?
Of course this wouldn't work for legacy code...
Thankfully Swift, Rust, Go, Scala, Typescript, and AtScript are all doing away with the spiral declaration syntax.
It's not a rhetoric question, I'd really want an answer as I'm contemplating the choice of language for and open-source project. And C++ is out because everybody has their own way of writing code in it and it's just too much work to police people into following a strong-handed style guide...
...so what would be left for an open source projects that has the goal of being able to squeeze the maximum performance without having to drop into a lower level language for the most performance sensitive parts?