Why do custom getters and setters have different syntax?


#1

Totally nonsense example   // this works   public var i : Int = 1   get() = $i + 5   set(value) {            $i = value + 5   }

why isn’t
get() { $i + 5}
or
get() { return $i + 5}

Just trying to understand this.

thanks

Eric


#2

It's a form of Single Expression Function - (http://confluence.jetbrains.com/display/Kotlin/Functions). It's not specific to getters. Below's code works for example

public class Data{    public var i : Int = 1   get(){ return $i + 5 }   set(value) = $i = value + 5    }

fun main(args : Array<String>) {
  val data = Data()
  data.i = 5
  println(“Hello, world! ${data.i}”)
}


#3

Thanks

I think I need to read the grammar. This makes sense.