Created
September 19, 2011 16:44
-
-
Save milessabin/de58f3ba7024d51dcc1a to your computer and use it in GitHub Desktop.
No Nothings
This file contains hidden or 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
def unexpected : Nothing = sys.error("Unexpected invocation") | |
// Encoding for "A is not a subtype of B" | |
trait <:!<[A, B] | |
// Uses ambiguity to rule out the cases we're trying to exclude | |
implicit def nsub[A, B] : A <:!< B = null | |
implicit def nsubAmbig1[A, B >: A] : A <:!< B = unexpected | |
implicit def nsubAmbig2[A, B >: A] : A <:!< B = unexpected | |
// Type alias for context bound | |
type |¬|[T] = { | |
type λ[U] = U <:!< T | |
} | |
def something[T: |¬|[Nothing]#λ](t : T) = t | |
val smth1 = something(23) // OK | |
val smth2 = something("foo") // OK | |
val smth3 = something(error("Boom!")) // Does not compile |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
implicit def nsubAmbig1[A, B >: A] : A <:!< B = unexpected
implicit def nsubAmbig2[A, B >: A] : A <:!< B = unexpected
why this duplication?