Skip to content

Instantly share code, notes, and snippets.

@wheaties
Created May 29, 2012 00:29
Show Gist options
  • Save wheaties/2821864 to your computer and use it in GitHub Desktop.
Save wheaties/2821864 to your computer and use it in GitHub Desktop.
Type Predicates that just ain't working...
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