Created
May 29, 2012 00:29
-
-
Save wheaties/2821864 to your computer and use it in GitHub Desktop.
Type Predicates that just ain't working...
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
sealed trait TypeBoolean | |
sealed trait True extends TypeBoolean | |
sealed trait False extends TypeBoolean | |
trait Conditional{ | |
type Result <: TypeBoolean | |
} | |
abstract class TypePredicate2[A,B,Cond[_,_] <: Conditional,Implicit[_,_]] extends Conditional{ | |
implicit def pred[Child,Parent](implicit ev:Implicit[Child,Parent]):Cond[Child,Parent] | |
abstract class Answer(implicit ev: Cond[A,B]) extends Conditional{ | |
type Result = Cond[A,B]#Result | |
} | |
type Result = Answer#Result | |
} | |
sealed abstract class SubType[A,B] extends Conditional | |
object IsSubtypeOf{ | |
implicit def default[Child,Parent] = new SubType[Child,Parent]{ type Result = False } | |
} | |
abstract class IsSubtypeOf[A,B] extends TypePredicate2[A,B,SubType,<:<]{ | |
implicit def pred[Child,Parent](implicit ev: <:<[Child,Parent]):SubType[Child,Parent] = | |
new SubType[Child,Parent] with (Child => Parent){ def apply(x: Child):Parent = x | |
type Result = True | |
} | |
} | |
class Parent | |
class Child extends Parent | |
class Foo{ | |
def bar[A <: TypeBoolean](implicit ev: =:=[A,True]) = 1 | |
def ohNoes() = bar[IsSubtypeOf[Child,Parent]#Result] | |
} | |
[error] could not find implicit value for parameter ev: =:=[SubType[Child,Parent]#Result,True] | |
[error] def ohNodes() = bar[IsSubtypeOf[Child,Parent]#Result] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment