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