What unexpected exception do you think I get here ...
# getting-started
d
What unexpected exception do you think I get here (🧵)
Copy code
package com.stochastictinkr

@JvmInline
value class Value<B>(val value: B)

fun <W> Value<String>.replace(transform: (String) -> W): Value<W> {
    return Value(transform(value))
}

data class Holder<D : Value<*>>(val name: String, val descriptor: D) {
    inline fun <D2 : Value<*>> inlineModify(block: D.() -> D2) = Holder(name, descriptor.block())
    fun <D2 : Value<*>> notInlineModify(block: D.() -> D2) = Holder(name, descriptor.block())
}

fun <W, D : Value<String>> D.inlineCreateAndModify(name: String, wrapper: (String) -> W) =
    Holder(name, this).inlineModify { replace(wrapper) }

fun <W, D : Value<String>> D.notInlineCreateAndModify(name: String, wrapper: (String) -> W) =
    Holder(name, this).notInlineModify { replace(wrapper) }

fun main() {
    val descriptor = Value<String>("32")
    println(descriptor.notInlineCreateAndModify("notInline", String::toInt))
    println(descriptor.inlineCreateAndModify("isInline", String::toInt))
}
Copy code
Holder(name=notInline, descriptor=32)
Exception in thread "main" java.lang.ClassCastException: class com.stochastictinkr.Value cannot be cast to class java.lang.String (com.stochastictinkr.Value is in unnamed module of loader 'app'; java.lang.String is in module java.base of loader 'bootstrap')
	at com.stochastictinkr.BrokenKt$main$2.invoke(Broken.kt:24)
	at com.stochastictinkr.BrokenKt.replace-AWdvzEo(Broken.kt:7)
	at com.stochastictinkr.BrokenKt.inlineCreateAndModify-Fmre_BE(Broken.kt:16)
	at com.stochastictinkr.BrokenKt.main(Broken.kt:24)
	at com.stochastictinkr.BrokenKt.main(Broken.kt)
This works
Value
is not a value class.