Skip to content

Instantly share code, notes, and snippets.

@kaychaks
Last active September 19, 2016 01:44
Show Gist options
  • Save kaychaks/626788e33f39037f3dc475004a6eb79c to your computer and use it in GitHub Desktop.
Save kaychaks/626788e33f39037f3dc475004a6eb79c to your computer and use it in GitHub Desktop.
Typeclass with multiple parameters issue
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)
@kaychaks
Copy link
Author

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 objects being considered a different type than the types that they represent ?

@kaychaks
Copy link
Author

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