Skip to content

Instantly share code, notes, and snippets.

@evacchi
Last active January 4, 2016 10:00
Show Gist options
  • Save evacchi/2bae2a1f78917c783cd1 to your computer and use it in GitHub Desktop.
Save evacchi/2bae2a1f78917c783cd1 to your computer and use it in GitHub Desktop.
Scala-Prolog
trait John
trait Carl
trait Tom
trait Child[T,U]
trait GrandChild[T,U]
implicit val john_carl = new Child[John,Carl]{}
implicit val carl_tom = new Child[Carl,Tom ]{}
implicit def grandChild[X,Y,Z](
implicit
xy: Child[X,Y],
yz: Child[Y,Z]
) = new GrandChild[X,Z] {}
> implicitly[ GrandChild[John, Tom] ]
// (compiles; returns the fact instance)
> implicitly[ GrandChild[John, Carl] ]
Compilation Failed
// both compile:
> implicitly[ GrandChild[John, _]]
> implicitly[ GrandChild[John, X forSome { type X }] ]
> implicitly[ GrandChild[_, Tom] ] // does not compile
trait LowPriority {
implicit def grandChildFallback[X,Y,Z](
implicit
xy: Child[X,Y],
yz: Child[Y,Z]
) = new GrandChild[X,Z]{}
}
object Implicits extends LowPriority {
implicit def grandChild[X,Y,Z](
implicit
yz: Child[Y,Z],
xy: Child[X,Y]
) = new GrandChild[X,Z]{}
}
import Implicits._
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment