About property getters

Consider this:

class SomeClass {
   
   val someProperty: SomeType
      get() { /* ... */ }
}

Why not have property getters without parentheses?
Something like:

class SomeClass {
   
   val someProperty: SomeType
      get { /* ... */ }
}

Thank you for your attention,
João

3 Likes

…why should we? It’s just not Kotlin’s syntax for function declarations - that’s why.

You could point to an init block as being like a function definition without parens. Similarly, a lambda can be written without parens if it’s the only parameter to a function, or is not a parameter. So there are sort of precedents. But those aren’t quite the same thing.

You could instead call the parens a useful reminder of a function definition, which is less obvious in the case of getters and setters.

Personally, I don’t think there’s very much in it one way or another…

The bottom line is that after 14 years, Kotlin’s basic syntax is extremely unlikely to be changed unless there’s a very significant benefit, and I doubt saving two characters would count!

1 Like