Skip to content

Instantly share code, notes, and snippets.

@tiiime
Created June 23, 2021 02:59
Show Gist options
  • Save tiiime/e6124dada9958ae1386d9443938f2ad5 to your computer and use it in GitHub Desktop.
Save tiiime/e6124dada9958ae1386d9443938f2ad5 to your computer and use it in GitHub Desktop.
Kotlin property builder
package com.example.playground
class Builder {
val setName: PropertiesSetter<String, Builder> = PropertiesSetter(this)
val setAge: PropertiesSetter<Int, Builder> = PropertiesSetter(this)
fun build(){
setName.value
}
class PropertiesSetter<T, R>(private val r: R) {
var value:T? = null
operator fun invoke(t: T): R {
value = t
return r
}
}
}
val builder = Builder()
.setName("haha")
.setAge(15)
.build()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment