i’ve got a class like: ```class MyFoo( val ti...
# codereview
b
i’ve got a class like:
Copy code
class MyFoo(
    val title:  String,
    val client: Foo = (some default)
) {...}
I want to provide a default value of Foo when if it’s not provided, but it’s nontrivial to create a valid instance, and requires storing an instance variable e.g.
Copy code
val x = Bar()
client = Foo.builder().bar(x).baz(Baz.builder().bar(x).build()).build()
i’m struggling to figure out if this is possible with initializer / secondary constructors, without having to make
client
a nullable
var
.
e
Copy code
class MyFoo(
    val title: String,
    val client = run {
        val x = Bar()
        Foo.builder().bar(x).Baz(Baz.builder().bar(x).build()).build()
    }
)
should work, but for something as complex you should probably have a
fun defaultClient(title: String): Foo
instead
s
The default can come from a method call: e.g.:
val client:Foo = getDefaultClient()
, which could be a method in Foo's companion object, or anywhere else.
b
ah, welp, that makes sense
dunno how on earth i didn’t get to that myself, but… thanks!