title: Implementing a string class that does case insensitive comparisions in Scala
body: <p>I've got a number of classes with fields that are meant to be case insensitive, and I'd like to put the instances of these classes into HashMaps and look them up by string case insensitive.</p>
  
  <p>Instead of using toLowerCase every time I want to index an instance by its string, or look up an instance by its string, I've instead tried encapsulate this logic in a CaseInsensitiveString class:</p>
  
  <pre><code>/** Used to enable us to easily index objects by string, case insensitive
   * 
   * Note: this class preservse the case of your string!
   */
  class CaseInsensitiveString ( val _value : String ) {
    override def hashCode = _value.toLowerCase.hashCode
    override def equals(that : Any) = that match {
      case other : CaseInsensitiveString =&gt; other._value.toLowerCase ==_value.toLowerCase
      case other : String =&gt; other.toLowerCase == _value.toLowerCase
      case _ =&gt; false
    }
    override def toString = _value
  }
  
  object CaseInsensitiveString {
    implicit def CaseInsensitiveString2String(l : CaseInsensitiveString) : String = if ( l ==null ) null else l._value
    implicit def StringToCaseInsensitiveString(s : String) : CaseInsensitiveString = new CaseInsensitiveString(s)
  
    def apply( value : String ) = new CaseInsensitiveString(value)
    def unapply( l : CaseInsensitiveString) = Some(l._value)
  }
  </code></pre>
  
  <p>Can anyone suggest a cleaner or better approach? </p>
  
  <p>One drawback I've come across is when using junit's assertEquals like this:</p>
  
  <pre><code>assertEquals("someString", instance.aCaseInsensitiveString)
  </code></pre>
  
  <p>It fails, saying it expected "someString" but got CaseInsensitiveString&lt;"someString">. </p>
  
  <p>If I reverse the order of the variables in the assertEquals, then it works, probably because its then calling the equals function on the class CaseInsensitiveString.  I currently work around this by keeping the order the same (so the expected one is actually the expected one) but call .toString on the CaseInsensitiveString:</p>
  
  <pre><code>assertEquals("someString", instance.aCaseInsensitiveString.toString)
  </code></pre>
  
  <p>This works too:</p>
  
  <pre><code>assertEquals(CaseInsensitiveString("someString"), instance.aCaseInsensitiveString)
  </code></pre>
  
  <p>Is it possible for me to add an implicit equals to String to solve this?  </p>