https://kotlinlang.org logo
Title
s

shirbal

06/25/2017, 8:47 AM
Is there a way to define a c’tor which will look like this one instantiating (not specific use case, just playing with the language): var myclass = MyClass { propertyA = “Val1” propertyB = “val2" … }
m

mgaetan89

06/25/2017, 8:52 AM
That's almost what Kotlin offers out of the box:
class MyClass(
    val propertyA: String? = null,
    val propertyB: String? = null
)

val myClass = MyClass(
    propertyA: "Val1",
    propertyB: "Val2"
)
s

shirbal

06/25/2017, 9:22 AM
I meant with curly braces {} and without commas
m

mgaetan89

06/25/2017, 9:27 AM
So you can try this:
MyClass().apply {
    propertyA = "Val1"
    propertyB = "Val2"
}
👍 1
t

tschuchort

06/25/2017, 1:54 PM
Maybe you can also do something with typesafe builders, similar to the library anko
s

shirbal

06/25/2017, 10:03 PM
Thanks guys. @tschuchort, I think this is what I was looking for.