Ben Madore
09/30/2021, 5:48 PMclass 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.
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
.ephemient
09/30/2021, 6:02 PMclass 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
insteadsbyrne
09/30/2021, 6:04 PMval client:Foo = getDefaultClient()
, which could be a method in Foo's companion object, or anywhere else.Ben Madore
09/30/2021, 6:05 PMBen Madore
09/30/2021, 6:05 PM