Skip to content

Instantly share code, notes, and snippets.

@esarbe
Last active November 3, 2019 00:17
Show Gist options
  • Save esarbe/8c2ce01501612f8d9eedfc4914f16a37 to your computer and use it in GitHub Desktop.
Save esarbe/8c2ce01501612f8d9eedfc4914f16a37 to your computer and use it in GitHub Desktop.

Scala indentation syntax

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

What is proposed

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.

The problem

Why not the colon?

  • already overloaded

scala/scala3#7136

The Case For The Equal Sign

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)

the proposed role of = in indentation syntax

regularize syntax

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.

How to regularize Scala 3 indentation syntax

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment