Why does Kotlin use braces instead of whitespace?

It's great that Kotlin got rid of the semicolon at the end of each line, but why are there still braces? Given that we already use an IDE that indents the code to match the braces, why not get rid of the braces entirely and use the indentation for that directly? Many languages already do this and it works well. Code could look like this: package bottles

fun main(args: Array<String>)
  if (args.isEmpty)
  printBottles(99)
  else
  try
           printBottles(Integer.parseInt(args[0]))
  catch (e: NumberFormatException)
           System.err.println("You have passed ‘${args[0]}’ as a number of bottles, " +
                   “but it is not a valid integer number”)

fun printBottles(bottleCount: Int)
  if (bottleCount <= 0)
  println(“No bottles - no song”)
  return

  println(“The ‘${bottlesOfBeer(bottleCount)}’ songn”)

  bottles := bottleCount
  while (bottles > 0)
  bottlesOfBeer := bottlesOfBeer(bottles)
  print(“$bottlesOfBeer on the wall, $bottlesOfBeer.nTake one down, pass it around, “)
  bottles–
  println(”${bottlesOfBeer(bottles)} on the wall.n”)
  
  println(“No more bottles of beer on the wall, no more bottles of beer.n” +
           “Go to the store and buy some more, ${bottlesOfBeer(bottleCount)} on the wall.”)

fun bottlesOfBeer(count: Int): String
  “${when (count)
           0 -> “no more bottles”
           1 -> “1 bottle”
           else -> “$count bottles”} of beer”

///
  An excerpt from the Standard Library
///

// This is an extension property, i.e. a property that is defined for the
// type Array<T>, but does not sit inside the class Array
<T> Array<T>.isEmpty: Boolean get() := size() == 0

1 Like

My guess is that this is because Kotlin needs to fit well between Java and Scala.

Also indentation-aware syntax is very famous in the dynamic typing community (Python, Ruby, CoffeeScript), but maybe feared upon in other communities and languages. And it makes the lexer and parser harder to write and maintain.

There are also examples of important new languages that aim to provide a language for larger, industry-scale applications, but also use braces like Rust and TypeScript.

Kotlin does not use significant indentation because the designers of Kotlin do not believe that significant indentation is a good idea. It's possible to design a language that would be similar to Kotlin and would use significant indentation, but it's not the language that we designed, and it's not the language that we're ever going to change Kotlin into.

1 Like

Agree that whitespace would be nicer. Moving from Python to Kotlin was an improvement in so many ways - null safety, the “?” - the ability to pass lambdas for single-method classes. But these damn

            }
        }
    }
}

everywhere make everything look so messy and needlessly inflate the line count without adding any information that wasn’t already in the indentation.