An interesting choice, probably too late. Right now Scala should focus on reducing paper cuts and puzzlers. This said; if indentation syntax is comming, I want it to be an improvement rather than
extend optionality of braces
we already do it! This is already legal for single-line method definitions
def add(l: Int, r: Int): Int = l + r
// or even
def add(l: Int, r: Int): Int =
l + r
It's also legal for match-case blocks.
- already overloaded
Simple assignment, equal sign + brace
val foo = { 1 }
Simple assignment, dropping brace
val foo = 1
def fib(n) = {
if (n == 0) then 0
else if (n == 1) then 1
else fib(n-1) + fib(n-2)
}
def fib(n) =
if (n == 0) then 0
else if (n == 1) then 1
else fib(n-1) + fib(n-2)
class Rational:
def __init__(self, x, y):
self.numerator = x
self.denominator = y
class Rational(x: Int, y: Int) = {
val numerator = x
val denominator = y
}
class Rational(x: Int, y: Int) =
val numerator = x
val denominator = y
def fib(n):
if n == 0: return 0
elif n == 1: return 1
else: return fib(n-1) + fib(n-2)
Python syntax is easy because it is regular. Creating
def name(ref):
body1
body2
class name(ref):
body1
body2
The proposed Scala 3 indentation syntax isn't.
object Foo extends Qux:
body1
body2
class Foo(param: Ref):
body1
body2
def qux(param: Ref): Foo =
body1
body2
Defining methods stands out like a sore thumb.
Let's buy into =
. It's already in use and is not overloaded with meaning.
def foo(param: Ref): Foo =
body1
body2
body3
This looks just what we had in the colon-indentation-syntax.
object Bar extends Qux =
body
body
class Foo2(param: Ref) =
body
body