1 Sep 2006 17:13
Revised proposed syntax change in Scala
Martin Odersky <martin.odersky <at> epfl.ch>
2006-09-01 15:13:21 GMT
2006-09-01 15:13:21 GMT
Thanks to the many people who responded. The responses convinced me that
the change as proposed does not fly. Nevertheless I still feel a bit
unhappy with the syntax we have now. Here's an example I particularly
dislike:
var x: int
def incr(): unit = x = x + 1
This looks strange. Adding braces around the statement helps a bit, but
does not fully resolve the problem:
def incr(): unit = { x = x + 1 }
There are several things that feel wrong with this example:
1. The two equals signs, which each mean something different.
2. The first equals sign really makes little sense here. The result of
the right hand side is the unit value (), whereas incr() is certainly
different from (). OK, you might say, we are fuzzy with equals anyway;
it should only be reserved for pure functions without side-effects or
exceptional returns. True. But in the case of unit-returning functions
(aka procedures), it sticks out more than for other functions. This is
because for a procedure we do not care about the result, all we care
about are the side effects.
(The second equals sign makes no sense either, but is well established
to mean assignment, so we do not want to change this).
3. The concept of `unit' is a bit strange for a newcomer to Scala. Maybe
we should rename `unit' to `void' but I just can't bring myself to do
this, because `unit' certainly has a value (i.e., ()), so it is not void.
So here is a new proposal which
- solves all three problems listed above
- is backwards compatible with current Scala
We allow as new declaration forms:
def f(params)
def f(params) { stats }
The first form is a shorthand for
def f(params): unit
the second is a shorthand for
def f(params): unit = { stats }
With the new syntax the example could be formulated as:
def incr() { x = x + 1 }
The proposal gets rid of the two equals, saves three tokens in the
definition of a procedure, and also gives a syntax where there is no
need to mention `unit'. Also, it does not break any existing code.
Opinions?
-- Martin
RSS Feed