Hello, how to manage variable datatype var aa=0 an...
# android
r
Hello, how to manage variable datatype var aa=0 and var aa=0.0 ? with out A*ny* key used?
v
This is Kotlin, not JavaScript. A variable always has the same type, either specified explicitly or implicitly by type inference.
f
Not sure what you are trying to do, but maybe you are looking for this:
Copy code
var number: Number = 0
check(number is Int)
number = 0.0
check(number is Double)
c
Another option is using an
Either
type to encapsulate a variable that could be an Int or a Double. Arrow has one (https://arrow-kt.io/docs/apidocs/arrow-core-data/arrow.core/-either/) but it’s probably easier to just write your own
Either
sealed class rather than bringing in all of Arrow just for this