Forces the compiler to treat the receiver object as having type T0
, even though doing so may violate type safety.
This method is useful when you believe you have type information the compiler doesn't, and it also isn't possible to check the type at runtime. In such situations, skipping type safety is the only option.
It is platform dependent whether asInstanceOf
has any effect at runtime. It might do a runtime type test on the erasure of T0
, insert a conversion (such as boxing/unboxing), fill in a default value, or do nothing at all.
In particular, asInstanceOf
is not a type test. It does **not** mean:
this match { case x: T0 => x case _ => throw ClassCastException("...")
Use pattern matching or isInstanceOf for type testing instead.
Situations where asInstanceOf
is useful:
T0
automaticallyBe careful of using asInstanceOf
when T0
is a primitive type. When T0
is primitive, asInstanceOf
may insert a conversion instead of a type test. If your intent is to convert, use a toT
method (x.toChar
, x.toByte
, etc.).
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4