Can't get to work with safe cast and operators


#1

I just cant use the code below,

var a=5
(a as? Char?:a as Int)        //This works fine
(a as? Char?:a as Int)+5     //But this throws an error
error: unresolved reference. None of the following candidates is applicable becaus
e of receiver type mismatch:

with some more lines

What’s wrong with this code?


#2

the type of (a as? Char?:a as Int) could be either Char? or Int depending on runtime type of a. So compiler can’t decide which type to use. IDE is usually giving a warning on the first cast since it can never succeed.

So, yes, everything works correctly and no, you should never do that.