Last active
January 4, 2016 10:00
-
-
Save evacchi/2bae2a1f78917c783cd1 to your computer and use it in GitHub Desktop.
Scala-Prolog
This file contains 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 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 | |
This file contains 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 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