Is there a way to instantiate an object while sett...
# announcements
b
Is there a way to instantiate an object while setting some properties on it? As an expression like:
Copy code
val a = Some() { b = "value" }
Or do I need multiple lines for that
s
What about
val a = Some(b = "value")
?
s
if the properties aren’t in the primary constructor and are public `var`s (and you don’t control this class), you could just
val a = Some().apply { b = "value" }
👍 1
b
❤️
thanks, looks great
s
given the caveats I mentioned I don’t know that it’ll necessarily solve the problem
but if works, great the constructor approach lets you set properties that are `val`s (assuming you have them defined in the primary ctor) and allow the parameters to be private instead
b
it’s all val. I don’t have an explicit contructor. The class is written in Java
I’m writing tests in Kotlin
m
If
Some
is Java, then
apply
is the way to go.