16

I would like to define a generic such that its type parameter does NOT extend a given type.

For example,

trait myTrait[T <: Throwable] {
  // ....
}

would define a trait where its type parameter extends Throwable. I want something like (not real Scala code):

trait myTrait[T Not(<:) Throwable] {
  // ....
}

Where the type type parameter does NOT extend Throwable. Is there a way to construct such a notion in Scala?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user997206
  • 281
  • 2
  • 4
  • 3
    Funny you should ask; Miles Sabin _just_ posted an answer to this on the scala-language list under the thread "Enforcing that the function returns something (other than Unit)": http://groups.google.com/group/scala-language/browse_thread/thread/e1242dfa7d65f599 – Rex Kerr Oct 16 '11 at 03:27

1 Answers1

23

You can do such a thing using implicits. Here's a trick from Miles Sabin on scala-language:

// 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 NOT[T] = {
 type Lambda[U] = U <:!< T
}

// foo does not accept T of type Unit
def foo[T : NOT[Unit]#Lambda](t : T) = t
Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681
  • If you already use the `shapeless` library then you can do: `def foo[T : |¬|[Unit]#λ](t : T) = t` Or, in two lines: `type NotUnit[T] = |¬|[Unit]#λ[T]` and `foo[T: NotUnit](t: T) = t` – VasiliNovikov Dec 30 '14 at 01:32