Is there Scala equivalent of C# as
keyword?
var something = obj as MyClass;
Scala's asInstanceOf
throws java.lang.ClassCastException
:
val something = obj.asInstanceOf[MyClass]
After reading up on C# a bit, I realized you probably meant this:
val foo = if (bar.isInstaceOf[Foo]) bar.asInstanceOf[Foo] else null.asInstanceOf[Foo]
It should be noted that using null is discouraged in Scala. You should really do this:
val foo = if (bar.isInstaceOf[Foo]) Some(bar.asInstanceOf[Foo]) else None
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With