2012-11-27 67 views
1

有沒有人有處理這些類型的編譯錯誤的經驗?Scala GADT Skolem Playframework 2.1編譯錯誤。

case class BasicAuthRequest[A](account: Account, request: Request[A]) extends WrappedRequest(request) { 
    def asOpt[T](implicit fjs: Reads[T]): Option[T] = { 
     catching(classOf[RuntimeException]).opt(fjs.reads(JsNull)).filter { 
     case JsUndefined(_) => false 
     case _ => true 
    } } 
} 

我試圖編譯此playframework方法時得到從Scala編譯器該響應

[error] found : play.api.libs.json.JsUndefined 
[error] required: play.api.libs.json.JsResult[?T1] where type ?T1 <: T (this is a GADT skolem) 
[error]   case JsUndefined(_) => false 
[error]   ^
[error] one error found 
[error] (compile:compile) Compilation failed 
[error] Total time: 23 s, completed Nov 27, 2012 3:01:20 AM 

回答

0

fjs.reads(JsNull)返回JsResult [T],然後通過選擇

裹入選項

所以有與此代碼幾個問題:

  • 返回類型應該是選項[JsResult [T]
  • JsUndefined不是JsResult的子類,所以你不能在匹配中使用它
  • 編譯器錯誤信息確實是神祕的,你會介意提交一個問題到scala跟蹤器嗎?

這裏是代碼,我覺得你是想實現

case class Account() 

    case class BasicAuthRequest[A](account: Account, request: Request[A]) extends WrappedRequest(request) { 
    def asOpt[T](implicit fjs: Reads[T]): Option[T] = 
     scala.util.control.Exception.catching(classOf[RuntimeException]).opt(fjs.reads(JsNull)).flatMap(_.asOpt) 
    }