2016-04-14 7 views
1

それはScalaでそのような何かを/ 2.4を再生することが可能です:が2.4パラメータ化代数データ型JSONの検証を再生

sealed trait Location { val `type`: String } 
case class CityLocation(zip_code: String, override val `type`: String = "city") extends Location 
case class AddressLocation(
    society: Option[String], 
    first_name: String, 
    last_name: String, 
    ... 
    override val `type`: String = "address" 
) extends Location 

sealed trait Point[T <: Location] { val `type`: String; val location: T } 
case class ShippingPoint[T](override val location: T, override val `type`: String = "shipping") extends Point[T] 
case class PickupPoint[T](override val location: T, override val `type`: String = "pickup") extends Point[T] 

object CityLocation { 
    implicit val format = Json.format[CityLocation] 
} 
object AddressLocation { 
    implicit val format = Json.format[AddressLocation] 
} 
object ShippingPoint { 
    implicit def write[T] = Json.writes[ShippingPoint[T]] 
    implicit def read[T]: Reads[ShippingPoint[T]] = (
    (__ \ "location").read[T] and 
     (__ \ "type").read[String](exactWordRead("shipping", "error.route.shipping.type")) 
)(ShippingPoint[T].apply _) 
} 
object PickupPoint { 
    implicit def write[T] = Json.writes[ShippingPoint[T]] 
    implicit def read[T]: Reads[PickupPoint[T]] = (
    (__ \ "location").read[T] and 
     (__ \ "type").read[String](exactWordRead("pickup", "error.route.pickup.type")) 
)(PickupPoint[T].apply _) 
} 

今のところコンパイルされません。

exactWordReadメソッドは次のように定義されます。

def exactWordRead(word: String, errorMessage: String): Reads[String] = Reads.constraints.pattern(s"^$word${"$"}".r, errorMessage) 

編集

私が前方にこの答えhttps://stackoverflow.com/a/29834113/2431728のおかげでステップをメイドと思われる:

sealed trait Location { val `type`: String } 
case class CityLocation(zip_code: String, override val `type`: String = "city") extends Location 
case class AddressLocation(
    society: Option[String], 
    first_name: String, 
    last_name: String, 
    ... 
    override val `type`: String = "address" 
) extends Location 

sealed trait Point[T <: Location] { val location: T; val `type`: String } 
case class ShippingPoint[T](override val location: T, override val `type`: String = "shipping") extends Point[T] 
case class PickupPoint[T](override val location: T, override val `type`: String = "pickup") extends Point[T] 

object CityLocation { 
    implicit val format = Json.format[CityLocation] 
} 
object AddressLocation { 
    implicit val format = Json.format[AddressLocation] 
} 
object ShippingPoint { 
    implicit def format[T: Format]: Format[ShippingPoint[T]] = (
    (__ \ "location").format[T] and 
     (__ \ "type").format[String](exactWordRead("shipping", "error.route.shipping.type")) 
)(ShippingPoint.apply, unlift(ShippingPoint.unapply)) 
} 
object PickupPoint { 
    implicit def format[T: Format]: Format[PickupPoint[T]] = (
    (__ \ "location").format[T] and 
     (__ \ "type").format[String](exactWordRead("pickup", "error.route.pickup.type")) 
)(PickupPoint.apply, unlift(PickupPoint.unapply)) 
} 

しかし、私は 'まだコンパイルしていない。コンパイルエラーがある:あなたが持っている一方で、コンパイラによって言ったように

[error] type arguments [T] do not conform to trait Point's type parameter bounds [T <: services.coliswebApi.data.Location] 
[error] case class ShippingPoint[T](override val location: T, override val `type`: String = "shipping") extends Point[T] 
[error]                          ^
[error] type arguments [T] do not conform to trait Point's type parameter bounds [T <: services.coliswebApi.data.Location] 
[error] case class PickupPoint[T](override val location: T, override val `type`: String = "pickup") extends Point[T] 

答えて

1

私はあなたにも確かにこの

implicit def format[T <: Location: Format]: Format[ShippingPoint[T]] = (
     (__ \ "location").format[T] and 
     (__ \ "type").format[String](exactWordRead("shipping", "error.route.shipping.type")) 
    )(ShippingPoint.apply, unlift(ShippingPoint.unapply)) 
+1

それはコンパイルする!タアンクス= D –

2

sealed trait Point[T <: Location] 

...と反対側に、

case class ShippingPoint[T](???) extends Point[T] 
case class PickupPoint[T](???) extends Point[T] 

しかし、何もでShippingPointPickupPointの宣言は、タイプパラメータがという制約と互換性があり、Pointを拡張する必要があることを証明しています。

これを修正する必要があります。

case class ShippingPoint[T <: Location](???) extends Point[T] 
case class PickupPoint[T <: Location](???) extends Point[T] 
+0

ようなあなたの形式であなたのタイプをバインドする必要があると思います。 今はコンパイルされない形式です。あなたはそれを修正する方法について考えていますか? –

+1

原因は同じです。 – cchantep

+0

確かに。助けてくれてありがとう=) –

関連する問題