3 ms·
This comes up in languages where everything is an expression, nothing is a statement. Because everything is an expression that needs to be evaluated, everything
by moron4hire 2y ago
This comes up in languages where everything is an expression, nothing is a statement. Because everything is an expression that needs to be evaluated, everything has a return type. You can't have something that doesn't return literally nothing (void), you have to return an empty expression. Thus, unit.
Requiring all language constructs be expressions and eliminating statements means that you avoid a lot of duplicate effort in the language design.
For example, in C-like language you have if-else statements and ternary expressions. The ternary expression does "the same thing": as condition statements, but it also evaluates to a value. So in functional programming languages, you just have the one kind of conditional expression, and then maybe some syntactic sugar to morph it into more ergonomic forms.