3 ms·
If you had never heard about C and its legacy, and were to create your own programming language, would you come up with a syntax like C's? The structure 'term:
by teo_zero 4y ago
If you had never heard about C and its legacy, and were to create your own programming language, would you come up with a syntax like C's?
The structure 'term: definition' is pretty common in many natural languages and has a lot of advantages: all terms are aligned, you can sort them, you can grep the term you're looking for, etc.
I even think it should be extended to functions, too. That 'def' ruins the harmony. Instead of
def factorial(int) -> int
it should be
factorial: fn(int) -> int
You trade a keyword ('def') for another ('fn') but open the way to lambdas (just the part after the colon).
I agree that the -> serves no purpose, it could be made optional. Alternatively one could require the -> and dismiss the 'fn' keyword, like this:
factorial: (int) -> int
It's slightly harder for the parser who has to wait until the -> to understand what it's reading, but it's doable.
About putting the return type after the parameters, that absolutely reflects how I reason about functions: I will want to first examine its input, and then its output. Even the man pages for common functions and commands explain the parameters first, and leave the return value last.