title: How can I check if Any value isEmpty?
body: <p>I have the following trait and class (actually this is a simplification, the real code is written in Java and is outside of my control):</p>
  
  <pre><code>trait BusinessTermValue {
    def getValue: Any
  }
  
  class BusinessTermValueImpl(override val getValue: Any) extends BusinessTermValue
  </code></pre>
  
  <p>Now I'm trying to improve my API without touching the original code (Pimp My Library pattern):</p>
  
  <pre><code>package object businessterms {
  
    implicit final class BusinessTermValueSupport(val btv: BusinessTermValue) extends AnyVal {
  
      def isDefined(): Boolean = btv != null &amp;&amp; btv.value != null
  
      def isEmpty(): Boolean = isDefined() &amp;&amp; (btv.value match {
        case s: String =&gt; s.isEmpty
        case l: Traversable[_] =&gt; l.isEmpty
        case c: java.util.Collection[_] =&gt; c.isEmpty
        case _ =&gt; false
      })
  
      def getAs[T](): T = btv.value.asInstanceOf[T]
  
    }
  
    object BusinessTerm {
      def apply(value: Any): BusinessTermValue = new BusinessTermValueImpl(value)
    }
  
  }
  </code></pre>
  
  <p>It works quite well:</p>
  
  <pre><code>println(BusinessTerm("A String").isEmpty) // false
  println(BusinessTerm(1).isEmpty) // false, Int can't be empty
  println(BusinessTerm(new Integer(1)).isEmpty) // false, Integer can't be empty
  println(BusinessTerm(List(1, 2, 3)).isEmpty) // false
  println(BusinessTerm(List(1, 2, 3).asJava).isEmpty) // false
  println(BusinessTerm("").isEmpty) // true
  println(BusinessTerm(List()).isEmpty) // true
  println(BusinessTerm(Seq()).isEmpty) // true
  println(BusinessTerm(Map()).isEmpty) // true
  println(BusinessTerm(List().asJava).isEmpty) // true
  </code></pre>
  
  <p>Still the pattern matching in <code>isEmpty</code> is cumbersome. Ideally I would like to use structural types and make sure that any type that implements <code>isEmpty</code> works with my API.</p>
  
  <p>Unfortunately the code below doesn't work. The variable <code>e</code> matches any type, even when <code>value</code> does not define <code>isEmpty</code>:</p>
  
  <pre><code>def isEmpty(): Boolean = isDefined() &amp;&amp; (btv.value match {
    case e: { def isEmpty(): Boolean } =&gt; e.isEmpty
    case _ =&gt; false
  })
  </code></pre>
  
  <p>Is there a way to delegate <code>isEmpty</code> only when the underlying <code>value</code> implements it? </p>