Ahmed
01/20/2024, 10:14 PMinit
question made me wonder if its okay to do the builder pattern as
class MyClassBuilder(init: MyClass.() -> Unit) {
private val myClass: Myclass
init {
myClass = MyClass().init()
}
fun build() = myClass
}
Maksym M.
01/20/2024, 11:11 PMAhmed
01/21/2024, 1:16 AMclass MyClassBuilder(init: MyClass.() -> Unit) {
private val builder: MyClass.() -> Unit
init {
builder = init
}
fun build() = MyClass().builder()
}
It addresses the lazy, its still type safe(?), makes the attributes of MyClass configureable. No need for constructors in MyClass as the fields can be private and expose public methods/fields. Wouldn't a dsl-like syntax nicer/ more readable?phldavies
01/21/2024, 2:19 PMMyClass
looks to be mutable, how is this any better than MyClass().apply { ...}
?Ahmed
01/21/2024, 2:21 PMMaksym M.
01/21/2024, 2:25 PMAhmed
01/21/2024, 6:29 PM<T> T.apply(block: T.() -> Unit): T
and its the same thing in principal.
Defeats the purpose of MyClassBuilder(init: MyClass.() -> Unit)
something like this altogether.
And yes, a named arg constructor makes more sense.
How would you approach building A that has B as a dependency. I can see it getting ugly pretty fast.
Class B(val id: Int = 0)
Class A(val fName: String = "John", val lName: String = "Doe", val class: B)
Maksym M.
01/21/2024, 6:32 PMAhmed
01/21/2024, 6:46 PMdata classes
may need to be passed in some object after network/disk IO.
If it becomes quite large, is it still okay to use a named arg? Or is there a different approach?
e.g.
Class Child(b: B, c: C, d: D, e: E, f: F, g: G, h: H): Base (b = b, c = c, d = d, e = e, f = f, g = g, h = h)
Maksym M.
01/21/2024, 7:24 PMDaniel Pitts
01/21/2024, 11:34 PMAhmed
01/22/2024, 7:55 AM