Created
September 19, 2011 16:44
-
-
Save milessabin/de58f3ba7024d51dcc1a to your computer and use it in GitHub Desktop.
No Nothings
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
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 |
wow... I didn't think it was possible...
Nice.
It looks like you can replace with |¬| with just ¬. Any particular reason for the bars?
implicit def nsubAmbig1[A, B >: A] : A <:!< B = unexpected
implicit def nsubAmbig2[A, B >: A] : A <:!< B = unexpected
why this duplication?
@epiphyllum because otherwise, this trick doesn't work 😄
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
why exactly is this not in the standard library?