Skip to content

Instantly share code, notes, and snippets.

@libetl
Created October 17, 2018 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save libetl/a28e517ab20c7c0894adc62da0aa7dc9 to your computer and use it in GitHub Desktop.
Save libetl/a28e517ab20c7c0894adc62da0aa7dc9 to your computer and use it in GitHub Desktop.
simplest kotlin builder
fun main(args: Array<String>) {
val choosenMenu = menu {
fish
chips
hamburger
}
println(choosenMenu)
}
fun menu(init : MenuBuilder.() -> Unit) = MenuBuilder().apply(init).build()
data class Menu(val list: List<String>)
class MenuBuilder {
val list = mutableListOf<String>()
fun build () = Menu(list)
val hamburger : Unit get () {list += "Hamburger"}
val fish : Unit get () {list += "Fish"}
val chips : Unit get () {list += "Chips"}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment