title: Scala: Enforcing A is not a subtype of B
body: <p>I am trying to overload a method based on whether or not a parameter extends a given class, and have been having some trouble.  Using <a href="https://gist.github.com/milessabin/c9f8befa932d98dcc7a4">an approach by Miles Sabin</a>, I produced the following code:</p>
  
  <pre><code>object ExtendedGenericTypes {
  
    trait &lt;:!&lt;[A, B] // Encoding for "A is not a subtype of B"
  
    // Use ambiguity to rule out the cases we're trying to exclude
    implicit def nsubAmbig1[A, B &gt;: A]: A &lt;:!&lt; B = null
    implicit def nsubAmbig2[A, B &gt;: A]: A &lt;:!&lt; B = null
  
    // The implicit substitutions
    implicit def nsub[A, B]: A &lt;:!&lt; B = null
  }
  </code></pre>
  
  <p>And my use case:</p>
  
  <pre><code>import ExtendedGenericTypes._
  
  class Foo
  
  def foobar[T](x: T)(implicit ev: T &lt;:&lt; Foo) = "hello"
  def foobar[T](x: T)(implicit ev: T &lt;:!&lt; Foo) = 5
  
  println(foobar(new Foo()))
  </code></pre>
  
  <p>Unfortunately, this results in ambiguity, and the compiler does not know which of the two methods to invoke.  I'm looking for both an explanation as to why there is ambiguity in this case (as opposed to the other simpler cases outlined in Miles' gist) and also how to circumvent this hurdle.  Note that I need to perform this check on the parameter level (rather than defining one method and doing the check in the body) because I want to have different return types.</p>