Skip to content

Instantly share code, notes, and snippets.

@seratch
Created June 11, 2012 00:22
Show Gist options
  • Save seratch/2907824 to your computer and use it in GitHub Desktop.
Save seratch/2907824 to your computer and use it in GitHub Desktop.
Improved error message for generalized type constraints
class Foo[A, B <: Option[A]] {
@annotation.implicitNotFound(msg = "Should be Some!")
sealed abstract class =:=[From, To] extends (From => To) with Serializable
private[this] final val singleton_=:= = new =:=[Any,Any] { def apply(x: Any): Any = x }
object =:= {
implicit def tpEquals[A]: A =:= A = singleton_=:=.asInstanceOf[A =:= A]
}
type This = Foo[A, B]
type WithSome = Foo[A, Some[A]]
def foo(implicit shouldBeSome: This =:= WithSome) = println("foo")
}
class Bar[A, B <: Option[A]] {
type This = Bar[A, B]
type WithSome = Bar[A, Some[A]]
def bar(implicit shouldBeSome: This =:= WithSome) = println("foo")
}
object Main extends App {
/*
val foo = new Foo[String, Option[String]]
foo.foo
*/
// foo.scala:25: error: Should be Some!
// foo.foo
// ^
// one error found
val foo = new Foo[String, Some[String]]
foo.foo
/*
val bar = new Bar[String, Option[String]]
bar.bar
*/
// foo.scala:38: error: Cannot prove that Main.bar.This =:= Main.bar.WithSome.
// bar.bar
// ^
// one error found
val bar = new Bar[String, Some[String]]
bar.bar
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment