Proving that one typeclass is a subset of another in Scala

101 Views Asked by At

Say I have the following two typeclasses:

object Foo {
   sealed trait FooClass[A]
   implicit object FooString extends FooClass[String]
   implicit object FooInt extends FooClass[Int]       
}

object Bar {
  trait BarClass[A] //leave this one unsealed as we're attempting to prove that Foo is a subset of Bar, not the other way around.
  implicit object BarString extends BarClass[String]
  implicit object BarInt extends BarClass[Int]
  implicit object BarDouble extends BarClass[Double]
}

is it possible to provide a hint to the compiler that Foo is a subset of Bar somehow, such that the following would compile, without a change to the type signatures of either method?

class Outer(callee : Inner) {
  import Foo._
  def call[A : FooClass](a) : Unit = callee.call(a);
}

class Inner
  import Bar._
  def call[B : BarClass](b) : Unit = ();
}

val test = new Outer(new Inner)
test.call(123)
test.call("Hello World")

My main objective is to allow the Outer class to be entirely unaware of the existence of the BarClass typeclass - using the Inner class to abstract over it entirely. Is this possible? For those who are interested, there's some context over in this git repository - the commit descriptions have some more detailed exposition of the problem I'm trying to solve).

0

There are 0 best solutions below