-
-
Save milessabin/c9f8befa932d98dcc7a4 to your computer and use it in GitHub Desktop.
No Units
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
// 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 = null | |
implicit def nsubAmbig2[A, B >: A] : A <:!< B = null | |
// Type alias for context bound | |
type |¬|[T] = { | |
type λ[U] = U <:!< T | |
} | |
def foo[T, R : |¬|[Unit]#λ](t : T)(f : T => R) = f(t) | |
foo(23)(_ + 1) // OK | |
foo(23)(println) // Doesn't compile |
Ingenious! I encountered this problem just a few days ago.
Awesome! Fairly mind-bending, but indeed clever :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Devilishly clever!