Skip to content

Instantly share code, notes, and snippets.

@evilthreads669966
Last active February 26, 2022 15:28
Show Gist options
  • Save evilthreads669966/0376d1b63175116dbefc986733b4a77e to your computer and use it in GitHub Desktop.
Save evilthreads669966/0376d1b63175116dbefc986733b4a77e to your computer and use it in GitHub Desktop.
value class with computed property. value classes may not have generic type parameters. nor can they have more than one "backed" property. If Bar is treated as an integer value at compile time, then how does it hold on to a reference to the computed property? Or better yet, how does a primitive value have a reference to the computed property?
@JvmInline
value class Bar(private val _foo: Int){
val computed: Int
get() = _foo.toString().length
}
@Throws(AssertionError::class)
fun testBar(){
val arg = 22
val argLength = arg.toString().length
val bar = Bar(arg)
val length = bar.computed
assert(length == argLength)
assert(length > 0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment