Created
June 23, 2021 02:59
-
-
Save tiiime/e6124dada9958ae1386d9443938f2ad5 to your computer and use it in GitHub Desktop.
Kotlin property builder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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