I'm creating a programming language as a hobby, but I encountered a problem with designing its syntax. The problem is the conflict between the syntax for defining zero-argument functions and the syntax for reassignment operations. The function definition syntax is taken from Haskell, and looks like this:
name arguments = expression
A zero-argument function would obviously have this form: name = expression
. However, that poses a problem because, without context, the programmer could confuse such a definition with a reassignment operation (which has the same form, variable = expression
).
My solution is simple: ban functions with zero arguments. More precisely, such functions would now take a single value of the unit type as their argument. This is inspired by Scala, where functions that "don't return anything", actually return ()
(of type Unit
), which is a type with only one possible value that carries no useful information.
The definition would then look like this:
name () = expression
And a call would have the form name ()
-- note that '()' is not a parameter list, but rather the value of type Unit
. The above definition relies on pattern-matching, as () is not the name of the argument, but the only form it can take.
My question is whether such a design decision makes theoretical sense, and whether it would have some practical negative effects?
myRandom () = new Random
overmyRandom = new Random()
if I am reading you correctly.variable = expression
defines an unchangeable binding rather than assigning to a variable.unit -> foo
is a common type when declaring "This has some side-effects and computes afoo
"