Daniel Pitts
11/11/2023, 4:35 AMinterface Builder<T> {
fun initialValue(value: T)
fun otherMethodsThatWithoutT()
}
fun <T> build(block: Builder<T>.()->Unit) =
BuilderImpl<T>().apply(block)
val buildWithType = build { initialValue(0.0) } // T = Double
bal buildWithouType = build { } // Default T to Unit
Michael de Kaste
11/13/2023, 1:06 PMval build = BuilderImpl<Unit>()
fun build() = BuilderImpl<Unit>()
Daniel Pitts
11/14/2023, 2:44 AMfun <S> sketch(build: SketchBuilder<S>.() -> Unit) {
SketchBuilderImpl<S>().apply(build)
}
fun sketch(build: SketchBuilder<Unit>.() -> Unit) {
SketchBuilderImpl<Unit>().apply(build)
}
fun makeSketches() {
sketch {
// `this` is SketchBuilder<Unit>
}
sketch {
initialState("") // compiler error: type mismatch, expected Unit, got String
}
sketch<String> {
// `this` is SketchBuilder<String>, but annoying to have to put the type here
initialState("")
}
}
Daniel Pitts
11/14/2023, 2:46 AMDaniel Pitts
11/14/2023, 2:46 AMfun <S> sketch(build: SketchBuilder<S>.() -> Unit) {
SketchBuilderImpl<S>().apply(build)
}
val SketchBuilder<Unit>.stateless get() = Unit
fun makeSketches() {
sketch {
stateless
// `this` is SketchBuilder<Unit>
}
sketch {
initialState("")
// `this` is SketchBuilder<String>
}
}