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
Related
I have a lot of classes such as DataFrameFlow, TextFlow, RDDFlow. They all derive from base class Flow.
Now I want to write a function judgeFlow which can read from a path: String and return something representing exact Flow type from which I can create corresponding instance. The whole code seems like the following
def judgeFlow(path:String) = /*1*/ {
Flow.getStoreType(path) match {
case StoreType.tdw =>
DataFrameFlow
case StoreType.hdfs =>
TextFlow
}
}
def createFlow(typeInfo:/*2*/) = /*3*/{
new typeInfo()
}
However, I don't know how to write in place 1, 2 and 3.
EDIT
Knowing how to construct them is not enough here, because I also want the following:
pattern matching through typeInfo
some ways to do asInstanceOf
EDIT 2
Definition of Flow
abstract class Flow(var outputName: String) extends Serializable{
def this() = this("")
...
}
Definition of DataFrameFlow
class DataFrameFlow(d: DataFrame, path: String) extends Flow {
var data: DataFrame = d
def this(data: DataFrame) = this(data, "")
def this(path: String) = this(null, path)
def this() = this(null, "")
...
}
Pattern matching can't return different types from different cases. The type returned by pattern matching is the least upper bound of types returned in cases.
When someone wants to return different types, most probably he/she wants a type class.
sealed abstract class Flow
class DataFrameFlow extends Flow
class TextFlow extends Flow
class RDDFlow extends Flow
trait JudgeFlow[In] {
type Out <: Flow
def judgeFlow(in: In): Out
}
object JudgeFlow {
implicit val `case1`: JudgeFlow[???] { type Out = DataFrameFlow } = ???
implicit val `case2`: JudgeFlow[???] { type Out = TextFlow } = ???
implicit val `case3`: JudgeFlow[???] { type Out = RDDFlow } = ???
}
def judgeFlow[In](in: In)(implicit jf: JudgeFlow[In]): jf.Out = jf.judgeFlow(in)
But the trouble is that types are resolved at compile time. You seem to want to choose a case based on a value of string i.e. at runtime. So you can't return more specific types than just Flow at compile time.
flatMap with Shapeless yield FlatMapper not found
It's hard to guess your use case completely.
But using Scala reflection you can try
import scala.reflect.runtime.universe._
import scala.reflect.runtime.currentMirror
def judgeFlow(path:String): Type = {
Flow.getStoreType(path) match {
case StoreType.tdw =>
typeOf[DataFrameFlow]
case StoreType.hdfs =>
typeOf[TextFlow]
}
}
def createFlow(typeInfo: Type): Flow = {
val constructorSymbol = typeInfo.decl(termNames.CONSTRUCTOR).asMethod
val classSymbol = typeInfo.typeSymbol.asClass
val classMirror = currentMirror.reflectClass(classSymbol)
val constructorMirror = classMirror.reflectConstructor(constructorSymbol)
constructorMirror().asInstanceOf[Flow]
}
I'm trying to go functional, but when working on real world problems I'm struggling, I need helps with a few basics. I like the idea of a type class and to add more implicit types in future.
trait Query {
def queryDetails: QueryDetails
}
case class LocalQueryType(queryDetails: QueryDetails) extends Query
case class JdbcQueryType(queryDetails: QueryDetails) extends Query
def queryTypeFactory(queryDetails: QueryDetails): Query = {
queryDetails.platform match {
case c if queryDetails.platform.contains("-file://") => LocalQueryType(queryDetails)
case _ => JdbcQueryType(queryDetails)
}
}
Then I have a type class that looks for the local or Jdbc types, but it doesn't work as it is receiving only Query type.
I've tried using generics like:
def queryTypeFactory[T<:Query](queryDetails: QueryDetails): T = {
queryDetails.platform match {
case c if queryDetails.platform.contains("-file://") => LocalQueryType(queryDetails)
case _ => JdbcQueryType(queryDetails)
}
}
Adding Type Class:
trait QueryTask[A] {
def runQuery(a: A): String
}
object QueryTask {
def apply[A](implicit sh: QueryTask[A]): QueryTask[A] = sh
object ops {
def runQuery[A: QueryTask](a: A) = QueryTask[A].runQuery(a)
implicit class ShowOps[A: QueryTask](a: A) {
def runQuery = QueryTask[A].runQuery(a)
}
}
implicit val localQuery: QueryTask[LocalQueryType] =
instance(localQueryType => s"running local: ${localQueryType.queryDetails.command} on platform: ${localQueryType.queryDetails.platform}")
implicit val jdbcQuery: QueryTask[JdbcQueryType] =
instance(jdbcQueryType => s"running jdbc: ${jdbcQueryType.queryDetails.command} on platform: ${jdbcQueryType.queryDetails.platform}")
def instance[A](func: A => String): QueryTask[A] =
new QueryTask[A] {
def runQuery(a: A): String = func(a)
}
The idea is to not use the usual OO factory or strategy pattern.
Type class approach seems not to work in your use case.
Implicits are resolved at compile time. So in order to decide which instance you need QueryTask[LocalQueryType] or QueryTask[JdbcQueryType] compiler has to know whether type A is LocalQueryType or JdbcQueryType at compile time.
But it seems you decide that depending on whether queryDetails.platform.contains("-file://") or not i.e. at runtime.
It seems you need usual pattern mathing. You should use type class pattern when it's necessary.
I'm using two Scala libraries that both rely on implicit parameters to supply codecs/marshallers for case classes (the libraries in question are msgpack4s and op-rabbit). A simplified example follows:
sealed abstract trait Event
case class SomeEvent(msg: String) extends Event
case class OtherEvent(code: String) extends Event
// Assume library1 needs Show and library2 needs Printer
trait Show[A] { def show(a: A): String }
trait Printer[A] { def printIt(a: A): Unit }
object ShowInstances {
implicit val showSomeEvent = new Show[SomeEvent] {
override def show(a: SomeEvent) =
s"SomeEvent: ${a.msg}"
}
implicit val showOtherEvent = new Show[OtherEvent] {
override def show(a: OtherEvent) =
s"OtherEvent: ${a.code}"
}
}
The Printer for the one library can be generic provided there's an implicit Show for the other library available:
object PrinterInstances {
implicit def somePrinter[A: Show]: Printer[A] = new Printer[A] {
override def printIt(a: A): Unit =
println(implicitly[Show[A]].show(a))
}
}
I want to provide an API that abstracts over the details of the underlying libraries - callers should only need to pass the case class, internally to the API implementation the relevant implicits should be summoned.
object EventHandler {
private def printEvent[A <: Event](a: A)(implicit printer: Printer[A]): Unit = {
print("Handling event: ")
printer.printIt(a)
}
def handle(a: Event): Unit = {
import ShowInstances._
import PrinterInstances._
// I'd like to do this:
//EventHandler.printEvent(a)
// but I have to do this
a match {
case s: SomeEvent => EventHandler.printEvent(s)
case o: OtherEvent => EventHandler.printEvent(o)
}
}
}
The comments in EventHandler.handle() method indicate my issue - is there a way to have the compiler select the right implicits for me?.
I suspect the answer is no because at compile time the compiler doesn't know which subclass of Event handle() will receive, but I wanted to see if there's another way. In my actual code, I control & can change the PrinterInstances code, but I can't change the signature of the printEvent method (that's provided by one of the libraries)
*EDIT: I think this is the same as Provide implicits for all subtypes of sealed type. The answer there is nearly 2 years old, I'm wondering if it's still the best approach?
You have to do the pattern matching somewhere. Do it in the Show instance:
implicit val showEvent = new Show[Event] {
def show(a: Event) = a match {
case SomeEvent(msg) => s"SomeEvent: $msg"
case OtherEvent(code) => s"OtherEvent: $code"
}
}
If you absolutely need individual instances for SomeEvent and OtherEvent, you can provide them in a different object so they can be imported separately.
If Show is defined to be contravariant (i.e. as trait Show[-A] { ... }, with a minus on the generic type) then everything works out of the box and a Show[Event] is usable as a Show[SomeEvent] (and as a Show[OtherEvent] for that matter).
If Show is unfortunately not written to be contravariant, then we might have to do a little bit more juggling on our end than we'd like. One thing we can do is declare all of our SomeEvent values as simply Events, vis a vis val fooEvent: Event = SomeEvent("foo"). Then fooEvent will be showable.
In a more extreme version of the above trick, we can actually hide our inheritance hierarchy:
sealed trait Event {
def fold[X]( withSomeEvent: String => X,
withOtherEvent: String => X ): X
}
object Event {
private case class SomeEvent(msg: String) extends Event {
def fold[X]( withSomeEvent: String => X,
withOtherEvent: String => X ): X = withSomeEvent(msg)
}
private case class OtherEvent(code: String) extends Event {
def fold[X]( withSomeEvent: String => X,
withOtherEvent: String => X ): X = withOtherEvent(code)
}
def someEvent(msg: String): Event = SomeEvent(msg)
def otherEvent(code: String): Event = OtherEvent(code)
}
Event.someEvent and Event.otherEvent allow us to construct values, and fold allows us to pattern match.
A bit of a funky scenario, best outlined below.
Suppose I have sealed trait Painting[T], and then a bunch of more specialised variations, such as trait ImpressionistPainting[T] extends Painting[T] and so on, building a simple sealed type family.
I then have a class with a f bounded polymorphic type bound as such:
class Gallery[T <: Gallery[T]]()(implicit helper: GalleryHelper[T])
And a:
trait GalleryHelper[T <: Gallery[T]] {
def paintings: Set[Painting[_]]
}
object GalleryHelper {
implicit def materialize[T <: Gallery[T]]: GalleyHelper[T] = {
macro MyMacro.materialize[T]
}
def apply[T <: Gallery[T]]()(
implicit ev: GalleryHelper[T]
): GalleryHelper[T] = ev
}
So far this is very basic and straightforward foo bar code.
The whole goal of this setup is to macro materialise a helper list of items that I need to describe the gallery, or in this case arbitrarily typed "user content", as such:
class MyGallery extends Gallery[MyGallery] {
object `le-reve` extends CubistPainting[Picasso]
object starry_night extends PostImpressionistPainting[VanGogh]
// ..
}
Now with some macro compat fun, I want to macro materialise the helper in question and filter the members of T <: Gallery[T] to extract those which are MemberType <:< Painting[_].
All very straightforward, the below is more complex than simply type.decls.filter(_.typeSignature <:< typeOf[Filter] because it needs to return a list of all inherited members in the order they are written as well, given galleries extend other galleries for instance.
def findMembersWithBound[T : WeakTypeTag, Filter : TypeTag](
exclusions: Symbol => Option[Symbol] = { s: Symbol => Some(s) }
): Set[Symbol] = {
val tpe = weakTypeOf[T].typeSymbol.typeSignature
(
for {
baseClass <- tpe.baseClasses.reverse.flatMap(exclusions(_))
symbol <- baseClass.typeSignature.members.sorted
if symbol.typeSignature <:< typeOf[Filter]
} yield symbol
)(collection.breakOut)
}
So in an implicit macro, a basic traversal of module members for type T would need to filter by a sub type, in this case Painting[_], and then look at what specific type arguments where provided by the user when extending a variant of Painting. The type family is sealed, so users extend a sub class of Painting with object, never Painting[_] directly, if this is relevant in any way.
#macrocompat.bundle
class MyMacro(val c: blackbox.Context) {
import c.universe._
def materialize[T <: Gallery[T]]: Tree = {
val galleryTpe = weakTypeOf[T]
val fields = findMembersWithBound[T, Painting[_]](exclusions)
val colMembers = sourceMembers.map { member =>
val memberType = member.typeSignatureIn(galleryTpe)
memberType.baseClasses.find(colSymbol ==) match {
case Some(root) => {
// Herein lies the problem, typeArgs is Nil.
root.typeSignatureIn(memberType).typeArgs.headOption match {
case Some(colSignature) => colSignature
case None => c.abort(
c.enclosingPosition,
s"Could not find the artist for ${member.asModule.name}"
)
}
}
case None => c.abort(c.enclosingPosition, s"Could not find root painting type for ${member.asModule.name}")
}
}
}
The problem is that none of the original type arguments passed through to Painting are visible anymore, even though the typeSignature would apper to be evaluated in scope and so on, and I'm simply trying to make sure Van Gogh doesn't become the proverbial Waldo.
What is the correct API to dealias or however else make those typeArgs visible again? Currently an empty list.
Ok turns out there's a "well hidden" way to do this using asSeenFrom. The relevant part is:
root.typeSignature.typeParams match {
case head :: Nil => head.asType.toType.asSeenFrom(memberType, colSymbol)
case _ => c.abort(
c.enclosingPosition,
"Expected exactly one type parameter provided for painting type"
)
}
I have a small Scala/Neo4j application that links people and topics through "skilledAt" and "interestedIn" relations. It has a REST/Json Api (using Scalatra) and I ran into a typical type-erasure problem when I wanted to add an "asJson" method to List[Person] and List[Topic]. I would like to implement different Json serialization behaviour for the different content types but of course the types get erased. The best I've been able to come up with so far is the following runtime trick:
implicit def topicsOrPeopleAsJson[T](list: List[T]) = new {
def asJson: String = {
list match {
case head :: tail if (head.isInstanceOf[Topic]) => topicsToJson(list.asInstanceOf[List[Topic]])
case head :: tail if (head.isInstanceOf[Person]) => peopleToJson(list.asInstanceOf[List[Person]])
case _ => "[]"
}
}
private def peopleToJson(people: List[Person]) = {
...
}
private def topicsToJson(topics: List[Topic]) = {
...
}
}
This works just fine but I was wondering whether there was a better solution, maybe something including type classes, a topic I'm not very familiar with (yet).
Use another level of implicit (this is typeclasses indeed):
trait ListToJsonConverter[T] {
def asJson(l: List[T]) : String
}
implicit object PeopleToJsonConverter extends ListToJsonConverter[Person] {...}
implicit object TopicToJsonConverter extends ListToJsonConverter[Topic] {...}
implicit object DefaultJsonConverter extends ListToJsonConverter[Any] {
def asJson(l: List[Any]) = "[]"
}
implicit def topicsOrPeopleAsJson[T](list: List[T])(implicit ev : ListToJsonConverter[T]) = new {
def asJson = ev.asJson(list)
}
This may not be exactly what you asked for however. The converter will be chosen at compile time. So if you call with a list of person which the compiler knows only as a List[Any], it will not work as expected.
Why not do it the OO way?
trait JSONable {
def toJSON:String
}
class Person
class Topics
implicit def persontoJSONable(p:Person) = new PersonSerializer(p)
implicit def topicToJSONable(t:Topic) = new PersonSerializer(t)
class PersonSerializer(p:Person) extends JSONable {
override def toJSON = {
//...
}
}
class TopicSerializer(t:Topic) extends JSONable {
override def toJSON = {
//...
}
}
def ListAsJSON[T <% JSONable](l:List[T]) = {
l.map(_.toJSON)
}