Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
No Nothings
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
@rkuhn

This comment has been minimized.

Copy link

@rkuhn rkuhn commented Sep 19, 2011

why exactly is this not in the standard library?

@andyczerwonka

This comment has been minimized.

Copy link

@andyczerwonka andyczerwonka commented Sep 19, 2011

wow... I didn't think it was possible...

@deanwampler

This comment has been minimized.

Copy link

@deanwampler deanwampler commented Dec 17, 2014

Nice.

It looks like you can replace with |¬| with just ¬. Any particular reason for the bars?

@epiphyllum

This comment has been minimized.

Copy link

@epiphyllum epiphyllum commented Aug 9, 2017

implicit def nsubAmbig1[A, B >: A] : A <:!< B = unexpected
implicit def nsubAmbig2[A, B >: A] : A <:!< B = unexpected

why this duplication?

@vhutov

This comment has been minimized.

Copy link

@vhutov vhutov commented Dec 12, 2019

@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