is there a nice way to have an overloaded function...
# getting-started
a
is there a nice way to have an overloaded function that accepts all (nullable) unsigned types, and to call that function with
0u
? At the moment Kotlin doesn’t know which unsigned type to use, and errors with
Overload resolution ambiguity
.
closest workaround I can think of is to use different parameter names for each function
Copy code
fun operation(uByte: UByte?): Unit = operation(uByte.toString())
and while there’s still an error with
operation(0u)
, at least it can be specified with
operation(uLong = 0u)
l
It appears to be an issue (https://youtrack.jetbrains.com/issue/KT-46360) that has been present for some time.
0u.javaClass.typeName
outputs
kotlin.UInt
, yet it still prints this error on
operation(0u)
. You can use parameter names like you did with
operation(uLong = 0u)
, but you can also call one of the "casting" methods, such as
operation(0u.toUInt())
,
operation(0u.toUShort())
, etc. As for a generic method matching all unsingned types, I'm not sure how. Looking at the API, it doesn't seem like they extend any other types like
Number
, they only implement
Comparable<T>
.
a
thanks for finding that issue @Laertes Moustakas I’ve made an additional issue https://youtrack.jetbrains.com/issue/KT-55999