question about kotlin's language design:
why are the primary constructor parameters specified next to the class name? wouldnt it be way more consistent with secondary constructors and other functions to have them next to
init
?
t
Tim Fennis
12/31/2019, 2:49 PM
The simple answer is probably because a lot of Kotlin’s syntax is borrowed from Scala.
😉 1
l
LastExceed
12/31/2019, 2:50 PM
that doesn't really answer my question
t
Tim Fennis
12/31/2019, 2:52 PM
Well Kotlin used the Scala syntax, and Scala probably based their syntax on Lisp. It’s pretty common in functional languages to define data types this way.
l
LastExceed
12/31/2019, 2:53 PM
that may be so, but my question is: why? why is that common practice?
t
Tim Fennis
12/31/2019, 2:57 PM
Well one of the reason is that this way of defining a class combines both the definitino of the class, with the definition of the contructor and the definition of the properties.
data class User(val id: Int, val name: String)
is much shorter than defining a class, constructor and properties separately.