编写一个通用的演员功能Scala [英] Writing a generic cast function Scala

查看:69
本文介绍了编写一个通用的演员功能Scala的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图实现编写一个方法,将Any的值转换为特定类型并返回选项,而不是抛出像instanceOf这样的异常。 Scala的行为并不像我预料的那样:

$ $ $ $ $ $ $ $ $ $> def cast [A](value:Any):Option [A] =
{
try
{
Some(value.asInstanceOf [A])
} catch
{
case e:Exception =>无


$ / code $ / pre

测试:

  val stringOption:Option [String] = cast [String](2)
stringOption must beNone

会失败并显示错误

  java.lang。例外:'Some(2)'不是None 

有人知道为什么吗?

解决方案

在你的游行中擦除降雨。因此,在运行时,类型A不再是已知的,并且将 asInstanceOf [A] 编译为no-op。它只是让编译器相信所得到的值是A类型的,但实际上在运行时并不能保证。



尽管你可以使用Scala的清单来解决它,但。不幸的是,JVM对原始类型/装箱的处理迫使我们做一些额外的工作。下面的工作,尽管它不处理类型的弱一致性,意思是例如一个Int不被视为Long,所以 cast [Long](42)返回 None

  def cast [A:Manifest](value:Any):Option [A] = {
val erasure = manifest [A] match {
case Manifest.Byte => classOf [java.lang.Byte]
case Manifest.Short => classOf [java.lang.Short]
case Manifest.Char => classOf [java.lang.Character]
case Manifest.Long => classOf [java.lang.Long]
case Manifest.Float => classOf [java.lang.Float]
case Manifest.Double => classOf [java.lang.Double]
case Manifest.Boolean => classOf [java.lang.Boolean]
case Manifest.Int => classOf [java.lang.Integer]
case m => m.erasure
}
if(erasure.isInstance(value))Some(value.asInstanceOf [A])else None
}
pre>

I am trying to achieve to write a method that casts a value of Any to a specific type and returns option instead of throwing an exception like instanceOf. Scala does not behave like i have expected it:

def cast[A](value: Any): Option[A] =
{
  try
  {
    Some(value.asInstanceOf[A])
  } catch
  {
    case e: Exception => None
  }
}

The test:

val stringOption: Option[String] = cast[String](2)
stringOption must beNone

fails with the Error

java.lang.Exception: 'Some(2)' is not None

Somebody has an idea why?

解决方案

Erasure rains on your parade here. Therefore at runtime the type A is not known anymore and asInstanceOf[A] is compiled to a no-op. It just makes the compiler believe that the resulting value is of type A, but that is not actually ensured at runtime.

You can use Scala's manifests to work around it, though. Unfortunately the JVM's handling of primitive types / boxing forces us to do some extra work.

The following works, although it doesn't handle "weak conformance" of types, meaning that e.g. an Int is not considered a Long, so cast[Long](42) returns None.

def cast[A : Manifest](value: Any): Option[A] = {
  val erasure = manifest[A] match {
    case Manifest.Byte => classOf[java.lang.Byte]
    case Manifest.Short => classOf[java.lang.Short]
    case Manifest.Char => classOf[java.lang.Character]
    case Manifest.Long => classOf[java.lang.Long]
    case Manifest.Float => classOf[java.lang.Float]
    case Manifest.Double => classOf[java.lang.Double]
    case Manifest.Boolean => classOf[java.lang.Boolean]
    case Manifest.Int => classOf[java.lang.Integer]
    case m => m.erasure
  }
  if(erasure.isInstance(value)) Some(value.asInstanceOf[A]) else None
}

这篇关于编写一个通用的演员功能Scala的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆