I'm having an issue with my Inline value class. Th...
# getting-started
a
I'm having an issue with my Inline value class. This instance just wraps a String, eg.,
Copy code
@JvmInline
value class MyValueClass(private val value: String) {
    override fun toString() = value
}
I need the
toString()
override because there are callers in the frameworks I need that call it, and I don't want the extra markup:
MyValueClass(value="Some value")
I have an nullable argument of type MyValueClass:
Copy code
fun myFunction(currentValue: MyValueClass? = null)
When I pass no value, or even if I explicitly pass
null
, then
myFunction
receives
currentValue
as
"null"
. Is this an auto-promotion? When I explicitly pass a string value, compilation fails with a Type Mismatch, as I'd expect. But why does a null value silently become "null", instead of a null
MyValueClass
? (Or in bytecode, just a
String?
?
s
That sounds super weird! Can you share an example program that reproduces it?
k
You're probably finding that
println(currentValue)
prints "null" or that the fact that
null.toString() == "null"
.
a
Thanks for the responses! I've tried to repro in kotlinplayground with the code below, and it's all behaving as-expected. I had assumed something like that at first, but couldn't find it, so it looked to me like the inline value class was created with null.toString() But this simple test proves that's not it. I'll dig deeper into our own framework. Thanks very much for this push-back into the right direction !
[For the record, the debugger's overlay makes it look like this was passed as the string "null"]
y
Debugging might be, well, a bit bugged with value classes. I think that's something that's planned to be improved after K2 is released
a
Nice to hear! I was happy to discover that's just a nuance