Search code examples
scalaconstructorapplycase-class

Scala case class, modify a parameter in constructor


I have a case class

case class SiteID(channel: String, locale: String)

Locale can be "en-GB" or "en_GB" . So I want to replace the hyphen by underscore.

The idea is to transform , so there is this equality

SiteID("blabla","en-GB") == SiteID("blabla","en_GB")

I tried with the following code

case class SiteID(channel: String, locale: String)

object SiteID{

  def apply(channel: String, locale: String):SiteID =  SiteID(channel,locale.replace("-","_") )
}

Solution

  • You are probably calling recursively the apply method of the companion object. Try using the new operator.

     object SiteID {
        def apply(channel: String, locale: String):SiteID =
           new SiteID(channel,locale.replace("-","_") )
     }