Last active
September 19, 2016 01:44
-
-
Save kaychaks/626788e33f39037f3dc475004a6eb79c to your computer and use it in GitHub Desktop.
Typeclass with multiple parameters issue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Feedtype | |
trait Atom extends Feedtype | |
trait Rss2 extends Feedtype | |
case object Atom extends Atom | |
case object Rss2 extends Rss2 | |
trait Encoding | |
trait Xml extends Encoding | |
trait Json | |
case object Json extends Json | |
case object Xml extends Xml | |
trait Content | |
case class show[T <: Feedtype,E <: Encoding](str: String, tp: T, en: E) extends Content | |
trait TypeClass[C,D,E] { | |
def show(c: C, d: D, e:E): String | |
} | |
trait Service{ | |
def print[T,C,D](t: T,c:C, d:D)(implicit s: TypeClass[T,C,D]): String | |
} | |
object Service extends Service{ | |
def print[T,C,D](t:T, c:C, d:D)(implicit s: TypeClass[T,C,D]): String = | |
s.show(t,c,d) | |
} | |
implicit val t1 = new TypeClass[show[Atom,Xml], Atom, Xml] { | |
def show(c: show[Atom,Xml], d:Atom, e:Xml) = c.str | |
} | |
implicit val t2 = new TypeClass[show[Rss2,Xml], Rss2, Xml] { | |
def show(c: show[Rss2,Xml], d:Rss2, e:Xml) = "hi there " + c.str | |
} | |
val s1 = show("some show", Atom, new Xml {}) | |
Service.print(s1, s1.tp, s1.en) |
figured out that issue is that atom
and xml
being case objects
when they are being used as values to create s1
. If I use new atom {}
or new xml {}
then the execution is happening fine. However, I wonder why are the case object
s being considered a different type than the types that they represent ?
Got a superb reply to this issue to my StackOverflow post here
http://stackoverflow.com/questions/39559806/scala-typeclass-with-multiple-parameters-error
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Current error after making the
case class show
polymorphic on types is this