Skip to content

Instantly share code, notes, and snippets.

@leolin310148
Created March 22, 2023 07:22
Show Gist options
  • Save leolin310148/a162a147af80480650d1252fabfa77bd to your computer and use it in GitHub Desktop.
Save leolin310148/a162a147af80480650d1252fabfa77bd to your computer and use it in GitHub Desktop.
kotlin operation function return self can invoke forever
enum class Foo {
HELLO,
WORLD,
//
;
var i = 0
}
class FooBuilderSpec {
operator fun Foo.invoke(i: Int): Foo {
this.i++
return this
}
}
class FooBuilder(dsl: FooBuilderSpec.() -> Unit = {}) {
init {
FooBuilderSpec().also(dsl)
}
}
fun main() {
FooBuilder {
Foo.HELLO(1)(2)(3)(4)(5)(6)(7)(8)(9)(10).i
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment