Is there a way to define a c’tor which will look l...
# announcements
s
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
That's almost what Kotlin offers out of the box:
Copy code
class MyClass(
    val propertyA: String? = null,
    val propertyB: String? = null
)

val myClass = MyClass(
    propertyA: "Val1",
    propertyB: "Val2"
)
s
I meant with curly braces {} and without commas
m
So you can try this:
Copy code
MyClass().apply {
    propertyA = "Val1"
    propertyB = "Val2"
}
👍 1
t
Maybe you can also do something with typesafe builders, similar to the library anko
s
Thanks guys. @tschuchort, I think this is what I was looking for.