Language Design – Problems with Banning Zero-Argument Functions

language-designtheorytype-systems

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?

Best Answer

Sure, that makes sense theoretically. Especially in functional languages, having a function that takes no input is weird. Though I would encourage you to prevent declaring parameterless functions, not prevent them in your type system.

Consider parameter binding. If you have a unary function and bind a parameter to it, you would have a parameterless function even though you did not declare it. Having a special case to add a dummy parameter seems not great (but perhaps unavoidable).

Just using a different declaration operator than the equal sign seems like it would also be a viable alternative to deal with potential confusion (though parameterless functions will have impact elsewhere in the language).

Related Topic