Most programming languages (both dynamically and statically typed languages) have special keyword and/or syntax that looks much different than declaring variables for declaring functions. I see functions as just as declaring another named entity:
For example in Python:
x = 2
y = addOne(x)
def addOne(number):
return number + 1
Why not:
x = 2
y = addOne(x)
addOne = (number) =>
return number + 1
Similarly, in a language like Java:
int x = 2;
int y = addOne(x);
int addOne(int x) {
return x + 1;
}
Why not:
int x = 2;
int y = addOne(x);
(int => int) addOne = (x) => {
return x + 1;
}
This syntax seems more natural way of declaring something (be it a function or a variable) and one less keyword like def
or function
in some languages. And, IMO, it is more consistent (I look in the same place to understand the type of a variable or function) and probably makes the parser/grammar a little bit simpler to write.
I know very few languages uses this idea (CoffeeScript, Haskell) but most common languages have special syntax for functions (Java, C++, Python, JavaScript, C#, PHP, Ruby).
Even in Scala, which supports both ways (and has type inference), it more common to write:
def addOne(x: Int) = x + 1
Rather than:
val addOne = (x: Int) => x + 1
IMO, atleast in Scala, this is probably the most easily understandable version but this idiom is seldom followed:
val x: Int = 1
val y: Int = addOne(x)
val addOne: (Int => Int) = x => x + 1
I am working on my own toy language and I am wondering if there are any pitfalls if I design my language in such a way and if there are any historical or technical reasons this pattern is not widely followed?
(int => int) addOne = (x) => {
is a lot more "special" and "complex" thanint addOne(int) {
...