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]
您可以使用模式匹配,如下所述: 如何在Scala中转换变量?
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.