I am modelling a programming language for fun, and the syntax is heavily influenced by Scala - specifically function definitions.
I have encountered a design problem because my language does not differentiate between functions defined via the def
syntax (class methods) and anonymous functions assigned to values (created using =>
) - it removes the differences in both implementation and behaviour.
The result is that the following two definitions mean the same thing:
def square(x: Int) = x*x
val square = (x: Int) => x*x
There is no reason for the latter form (immediate anonymous function assignment) to be used in any normal situation - it's simply possible to use it instead of the def
form.
Would having such duplicate syntax for defining named functions hurt the orthogonality of the language or some other design aspect?
I prefer this solution because it allows for short and intuitive definitions of methods and named functions (via def
), and short definitions of anonymous functions (using =>
).
Edit: Scala does differentiate between the two - anonymous functions are not the same as methods defined with def
in Scala. The differences are relatively subtle though - see the posts I linked before.
However, assigning existing functions
seems to be missing the end of the sentenceval
notation?fun
to define a recursive function.def
is. It's just a side effect of the fact that an anonymous function, say(x : Int) => x + 1
is an object, and objects can be assigned to values withval f = ...
. The language designers would have had to go out of their way to disallow the syntax. It's not quite the same as explicitly putting in the effort to support two different syntaxes that do (approximately) the same thing.