2

这是你去的那些时候之一,你是什么意思,它不编译?

这不是一个修辞问题:最短或最惯用的解决方法是什么?对于奖励积分,为什么有必要?

scala> import scala.util.Try
import scala.util.Try

scala> Try { getClass.getClassLoader loadClass "scala.util.Try" }

我希望这不会泄露游戏,但这里的信息是:

<console>:9: error: type mismatch;
 found   : Class[_]
 required: Class[?0(in value res0)] where type ?0(in value res0)
Note: Any >: ?0, but Java-defined class Class is invariant in type T.
You may wish to investigate a wildcard type such as `_ >: ?0`. (SLS 3.2.10)
              Try { getClass.getClassLoader loadClass "scala.util.Try" }

“调查”是指基础研究,还是仅仅应用文献中已有的技术?

我仍在等待错误消息的结尾,“留给读者作为练习”。

更新:

这是 Scala 2.10 的练习。

像往常一样,所有的好事都会降临在那些等待的人身上:

apm@mara:~/tmp$ skala
Welcome to Scala version 2.11.0-20130622-103744-990c2b024a (OpenJDK 64-Bit Server VM, Java 1.7.0_21).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import scala.language.existentials
import scala.language.existentials

scala> import scala.util.Try
import scala.util.Try

scala> Try { getClass.getClassLoader loadClass "scala.util.Try" }
res0: scala.util.Try[Class[?0]] forSome { type ?0 } = Success(class scala.util.Try)
4

2 回答 2

1

这肯定是一个重复的问题。也许有人可以指出这一点,或者确切地说类型推断没有做这里自然而然的事情。

有人留下了一个答案(似乎已经消失了?),并带有指向MacIver on existential types的有用链接。可能,我还需要MacGyver的帮助。

以下是我在去论坛的路上尝试的一些变体。

package classy
import scala.util._

class Foo

object Test {

  /* DNC
  def loadTry(n: String, loader: ClassLoader) = Try { loader loadClass n }
  def loadTry(n: String, loader: ClassLoader): Try[Class[_]] = Try { loader loadClass n }
  */

  def main(args: Array[String]) {
    val cl = getClass.getClassLoader
    println(loadTry("classy.Foo", cl))
    println(loadTry("classy.Bar", cl))

    println(cl loadClass "classy.Foo")
    println(loadOpt("classy.Foo", cl))
    println(loadTryAgain("classy.Foo", cl))
    println(loadTryYetAgain("classy.Foo", cl))
  }

  def loadOpt(n: String, loader: ClassLoader): Option[Class[_]] =
    try Some(loader loadClass n) catch {
      case _: Exception => None
    }
  def loadTryAgain(n: String, loader: ClassLoader): Try[Class[_]] = {
    val res: Option[Class[_]] = try Some(loader loadClass n) catch {
      case _: Exception => None
    }
    res match {
      case None    =>
        Failure(new RuntimeException(s"Warning: class not found: ${n})"))
      case Some(x) =>
        Success(x)
    }
  }
  def loadTryYetAgain(n: String, loader: ClassLoader): Try[Class[_]] = {
    val res = try loader loadClass n catch {
      case _: Exception => null
    }
    res match {
      case null =>
        Failure(new RuntimeException(s"Warning: class not found: ${n})"))
      case x    =>
        Success(x)
    }
  }
  def loadTry(n: String, loader: ClassLoader) =
    Try[Class[_]] {
      loader loadClass n
    } recoverWith {
      case e: Exception =>
        Failure(new RuntimeException(s"Warning: class not found: ${n} (${e.getMessage})"))
    }
}
于 2012-12-14T01:20:53.353 回答
1

尝试导致它。在 scala 2.10.0 中对我来说:

scala> import scala.util.Try
scala> val typeName = "scala.util.Try"    

错误:

scala> Try(Class.forName(typeName))
<console>:10: error: type mismatch;
 found   : Class[_]
 required: Class[?0(in value res1)] where type ?0(in value res1)
Note: Any >: ?0, but Java-defined class Class is invariant in type T.
You may wish to investigate a wildcard type such as `_ >: ?0`. (SLS 3.2.10)
              Try(Class.forName(typeName))
                               ^

没有错误:

scala> Try[Class[_]](Class.forName(typeName))
res2: scala.util.Try[Class[_]] = Success(class scala.util.Try)

catching也有同样的问题:

scala> import scala.util.control.Exception._

scala> catching(classOf[Throwable]) opt Class.forName(typeName)
<console>:13: error: type mismatch;
 found   : Class[_]
 required: Class[?0(in value res4)] where type ?0(in value res4)
Note: Any >: ?0, but Java-defined class Class is invariant in type T.
You may wish to investigate a wildcard type such as `_ >: ?0`. (SLS 3.2.10)
              catching(classOf[Throwable]) opt Class.forName(typeName)
于 2013-02-20T23:47:55.980 回答