2012-12-14 24 views
2

這是你去哪兒的時候,你是什麼意思,它不能編譯?甚至不能嘗試在Scala中加載一個類

這不是一個修辭問題:什麼是最短或idiomatickest修復?對於獎勵積分,爲什麼有必要?

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的練習。

像往常一樣,所有的好東西來那些等待:

[email protected]:~/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) 

回答

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})")) 
    } 
} 
1

Try引起它。我在斯卡拉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) 
相關問題