y
02/19/2023, 10:39 AMclass Foo and a class FooBuilder. the FooBuilder takes some parameters and tries to construct a Foo. then I can make a constructor in Foo that takes a FooBuilder.
now what I’d like to do is disallow creating a FooBuilder from outside the file (to avoid exposing the implementation and to avoid intermediate state and to avoid writing private everywhere in FooBuilder), and then have a function that creates a FooBuilder, and returns the result of the build (maybe Foo.companion.invoke)
but now I have to put the FooBuilder definition inside Foo, or else I’m exposing the private-in-file class FooBuilder. but that adds an extra indentation level and massive amounts of unrelated code to Foo. is there any other way?ephemient
02/19/2023, 10:45 AMclass FooBuilder internal constructor() if you don't want it constructed externally?y
02/19/2023, 10:51 AMFooBuilder as private, this solves it. thanks.y
02/19/2023, 11:16 AM@Suppress annotation I can use for the class to silence the could be private warnings?ephemient
02/19/2023, 11:21 AMy
02/19/2023, 11:22 AMephemient
02/19/2023, 11:23 AMStephan Schröder
02/19/2023, 5:14 PMy
02/19/2023, 5:27 PMFoo is pretty much just the result of the computationy
02/19/2023, 5:29 PMStephan Schröder
02/19/2023, 5:37 PMandylamax
02/19/2023, 6:42 PMephemient
02/19/2023, 7:14 PMGoetz Markgraf
02/20/2023, 6:43 AMFoo companion object that takes a lambda that uses a builder- or configuration-object as a receiver.
class Foo {
...
companion object {
fun build (FooBuilder.() -> Unit) : Foo {
...
}
}
}
The constructor of FooBuilder can be private because no-one than this build-function is ever creating an instance of it.ephemient
02/20/2023, 3:28 PM@JvmOverloads. see https://jakewharton.com/public-api-challenges-in-kotlin/:
Even if you are only appending properties to avoid breaking the component functions, these two signatures always change.
ephemient
02/20/2023, 3:30 PMJsonConfiguration(...) to Json(JsonBuilder.() -> Unit) pre-1.0 to ensure that that they could continue to evolve post-1.0