Answer the question
In order to leave comments, you need to log in
How does as work? What is the difference from type conversion?
I have never used this operator, but when I decided to try it, some difficulties arose.
I read that this is a type casting operator, that is, if I have 2 variables of different primitive types, then I can make one different, that is, make int from long, etc., but in fact I catch an error, I read on the Internet, did not understand anything, and did not find examples. Can you give a couple of examples of how to use it?
It seemed to me that type casting and conversion are the same, that is:
var x = 'c'
var y = x.toInt()
or
var y = x as Int
Answer the question
In order to leave comments, you need to log in
No, you misunderstood. as is about the parent-child relationship, which the Int, Long and Char you mentioned are not related to. That's why you get errors. What does this mean: you have a value that is of type "parent" and you want to cast it to type "child".
For example:
fun f(n: Number): Int{
return n as Int
}
f(10) // 10
f(10L) // ClassCastException
val x = 10
x is Int == true
x is Number == true
x is Any == true
x is Long == false
x as Int // redundent
val y: Long? = x as? Long //== null
val x: Any = ...
if (x is String) x.substring(0, 10)
if (x is Int) x + 10
x as Long // Long или ClassCastException
x + 10L // до этой точки выполнение дойдёт только в случае успешного каста.
sealed class B
class D1(val x: Int): B()
class D2(val y: String): B()
object D3: B()
fun f(b: B){
when (b){
is D1 -> b.x
is D2 -> b.y
is D3 -> D3
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question