Scala 类型化 Traits 和类型匹配
我目前正在 scala / lift 中开发电子商务应用程序,无法弄清楚如何在我的类型层次结构上正确匹配。我当前的实现是:
abstract trait BaseProduct[T <: BaseProduct[T]] extends MongoRecord[T] with ObjectIdPk[T] {
self:T =>
def whatever
...
}
abstract trait SimpleType[T <: BaseProduct[T]] extends BaseProduct[T] {
self:T =>
val producttype = "product/simple"
}
abstract trait ConfigurableType[T <: BaseProduct[T],E <: SimpleType[E]] extends BaseProduct[T] {
self: T =>
val producttype = "product/configurable"
}
class ClothingProduct extends SimpleType[ClothingProduct] {
def meta = ClothingProduct
}
class ConfigurableClothing extends ConfigurableType[ConfigurableClothing,ClothingProduct] {
val childmeta = ClothingProduct
val configurableFields = List(color,size)
def meta = ConfigurableClothing
}
有了这个,我想实现一个购物车,它应该只接受 SimpleType 类型(和 BaseProduct)的产品。它还应该实现一个函数,该函数采用任何类型的 BaseProduct 和模式匹配子句,该子句应该对简单类型或可配置类型做出反应。
我的第一个因类型错误而让我发疯的实现是:
object Cart extends SessionVar[List[CartItem]](List()) {
def addProduct(product:SimpleType[_],quantity:Int = 1) = {
this.get.partition(_.product.id.is == product.id.is) match {
case (Nil,xs) => this.set(CartItem(product,quantity) :: xs)
case (x,xs) => this.set(CartItem(product,x.head.quantity + quantity) :: xs)
}
}
}
case class CartItem(product:SimpleType[_], quantity:Int) {
def lineTotal:Double = quantity * product.price.is
}
使用以下函数添加购物车项目:
def addBasketLink(prod:BaseProduct[_]) = {
prod match {
case x:SimpleType[_] => Cart.addProduct(x)
case _ => S.notice("Sorry not possible to add to Basket")
}
我的主要问题是 SimpleType 提供的类型的“下划线”定义会导致类型违规错误。我目前无法弄清楚如何正确地进行所需的打字。如果有人能告诉我我的错误在哪里,并向我正确解释我如何正确处理类型化特征,那就太好了。
谢谢
I am currently working on a E-Commerce application in scala / lift and cannot figure out how to match correctly on my type hierarchy. my current implementation is:
abstract trait BaseProduct[T <: BaseProduct[T]] extends MongoRecord[T] with ObjectIdPk[T] {
self:T =>
def whatever
...
}
abstract trait SimpleType[T <: BaseProduct[T]] extends BaseProduct[T] {
self:T =>
val producttype = "product/simple"
}
abstract trait ConfigurableType[T <: BaseProduct[T],E <: SimpleType[E]] extends BaseProduct[T] {
self: T =>
val producttype = "product/configurable"
}
class ClothingProduct extends SimpleType[ClothingProduct] {
def meta = ClothingProduct
}
class ConfigurableClothing extends ConfigurableType[ConfigurableClothing,ClothingProduct] {
val childmeta = ClothingProduct
val configurableFields = List(color,size)
def meta = ConfigurableClothing
}
With this in place I want to implement a Shopping Cart which should only take Products of Type SimpleType (and BaseProduct). It should also implement a function that takes any kind of BaseProduct with a pattern matching clause that should react to wheater it is a Simple or a Configurable Type.
My first implementations which drives me crazy due to Type Errors was:
object Cart extends SessionVar[List[CartItem]](List()) {
def addProduct(product:SimpleType[_],quantity:Int = 1) = {
this.get.partition(_.product.id.is == product.id.is) match {
case (Nil,xs) => this.set(CartItem(product,quantity) :: xs)
case (x,xs) => this.set(CartItem(product,x.head.quantity + quantity) :: xs)
}
}
}
case class CartItem(product:SimpleType[_], quantity:Int) {
def lineTotal:Double = quantity * product.price.is
}
With to following function to add a Cart Item:
def addBasketLink(prod:BaseProduct[_]) = {
prod match {
case x:SimpleType[_] => Cart.addProduct(x)
case _ => S.notice("Sorry not possible to add to Basket")
}
My main issue is that "Underscore" definition of the supplied type to SimpleType which causes type violation errors. I am currently not able to figure out how to do the wanted typing right. It would be great it someone could tell me where my error is and properbly explain to me how I work with typed traits correctly.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论