Skip to content

Instantly share code, notes, and snippets.

@mandubian
Created December 17, 2012 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mandubian/4319123 to your computer and use it in GitHub Desktop.
Save mandubian/4319123 to your computer and use it in GitHub Desktop.
Scala pb simplified
package alpha {
trait AlphaReader[A] {
def read(s: String): A
}
object AlphaReader{
def apply[A](f: String => A): AlphaReader[A] = new AlphaReader[A] {
def read(s: String) = f(s)
}
}
trait AlphaCombinator[M[_]]{
def apply[A](ma: M[A]): M[Option[A]]
}
class AlphaOps[M[_], A](ma: M[A])(implicit c: AlphaCombinator[M]) {
def foo: M[Option[A]] = c(ma)
}
object Implicits {
implicit def toAlphaOps[M[_], A](ma: M[A])(implicit c: AlphaCombinator[M]) = new AlphaOps(ma)(c)
implicit def toAlphaCombinator = new AlphaCombinator[AlphaReader] {
def apply[A](ma: AlphaReader[A]): AlphaReader[Option[A]] =
new AlphaReader[Option[A]] { def read(s: String) = None }
}
}
}
package beta {
trait BetaReader[A] {
def read(s: String): A
}
object BetaReader{
def apply[A](f: String => A): BetaReader[A] = new BetaReader[A] {
def read(s: String) = f(s)
}
}
trait BetaCombinator[M[_]]{
def apply[A](ma: M[A]): M[Option[A]]
}
class BetaOps[M[_], A](ma: M[A])(implicit c: BetaCombinator[M]) {
def foo: M[Option[A]] = c(ma)
}
object Implicits {
implicit def toBetaOps[M[_], A](ma: M[A])(implicit c: BetaCombinator[M]) = new BetaOps(ma)(c)
implicit def toBetaCombinator = new BetaCombinator[BetaReader] {
def apply[A](ma: BetaReader[A]): BetaReader[Option[A]] =
new BetaReader[Option[A]] { def read(s: String) = None }
}
}
}
object Main extends App {
import alpha.Implicits._
import beta.Implicits._
val a1 = alpha.AlphaReader[Int](s => s.toInt)
val c1 = a1.foo
val a2 = beta.BetaReader[Int](s => s.toInt)
val c2 = a2.foo
}
// COMPILING ERROR
implicit_test.scala:102: error: type mismatch;
found : Main.a1.type (with underlying type alpha.AlphaReader[Int])
required: ?{val foo: ?}
Note that implicit conversions are not applicable because they are ambiguous:
both method toAlphaOps in object Implicits of type [M[_], A](ma: M[A])(implicit c: alpha.AlphaCombinator[M])alpha.AlphaOps[M,A]
and method toBetaOps in object Implicits of type [M[_], A](ma: M[A])(implicit c: beta.BetaCombinator[M])beta.BetaOps[M,A]
are possible conversion functions from Main.a1.type to ?{val foo: ?}
val c1 = a1.foo
^
implicit_test.scala:108: error: type mismatch;
found : Main.a2.type (with underlying type beta.BetaReader[Int])
required: ?{val foo: ?}
Note that implicit conversions are not applicable because they are ambiguous:
both method toAlphaOps in object Implicits of type [M[_], A](ma: M[A])(implicit c: alpha.AlphaCombinator[M])alpha.AlphaOps[M,A]
and method toBetaOps in object Implicits of type [M[_], A](ma: M[A])(implicit c: beta.BetaCombinator[M])beta.BetaOps[M,A]
are possible conversion functions from Main.a2.type to ?{val foo: ?}
val c2 = a2.foo
^
two errors found
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment