2016-09-06 42 views
0

我想的Xor一個HList映射到ValidatedNel一個HList並得到了一個錯誤:如何解決這個編譯錯誤與聚功能

scala> type Result[A] = Xor[String, A]  
defined type alias Result 

scala> type Validation[A] = ValidatedNel[String, A] 
defined type alias Validation 

scala> val r0 = Xor.right(0) 
r0: cats.data.Xor[Nothing,Int] = Right(0) 

scala> val r1 = Xor.left("xxx") 
r1: cats.data.Xor[String,Nothing] = Left(xxx) 

scala> import shapeless._ 
import shapeless._ 

scala> val rs = r0 :: r1 :: HNil 
rs: shapeless.::[cats.data.Xor[Nothing,Int],shapeless.::[cats.data.Xor[String,Nothing],shapeless.HNil]] = Right(0) :: Left(xxx) :: HNil 

scala> object toValidation extends (Result ~> Validation) { def apply[T](r: Result[T]): Validation[T] = r.toValidatedNel } 
defined object toValidation 

scala> rs map toValidation 
<console>:41: error: type mismatch; 
found : toValidation.type 
required: shapeless.Poly 
       rs map toValidation 

出了什麼問題上面的代碼,以及如何解決它?

回答

2

你可以仔細檢查你的進口?

我懷疑~>引用cats的自然轉換,而不是來自shapeless的Poly。

在定義toValidation對象之前導入shapeless.poly._應該消除該錯誤。

+0

謝謝。你可能是對的,但我現在有另一個問題。我會發布關於它的另一個問題。 – Michael

+0

這就是問題,順便說一下:http://stackoverflow.com/q/39374015/521070 – Michael