I'm trying to write some string utils to be able to do implicit conversions of the form "myString".doSpecialConversion and have a specialValue based on which doSpecialConversion works.
Attempt-1: Use a trait:
trait ConversionUtils {
// Need an overridable value
lazy val specialValue = ","
implicit class StringConversions(val s: String) {
def doSpecialConversion: Option[String] = if (s == specialValue) None else Some(s)
}
}
Trait works just fine but the problem is that its not static so multiple StringConversions will be created which is undesired. So I try to extend AnyVal which can't be done for a trait because Another limitation that is a result of supporting only one parameter to a class is that a value class must be top-level or a member of a statically accessible object.
Attempt-2: Use a singleton:
object ConversionUtils {
// Need an overridable value
lazy val specialValue = ","
implicit class StringConversions(val s: String) extends AnyVal {
def doSpecialConversion: Option[String] = if (s == specialValue) None else Some(s)
}
}
Question: How do I provide a Util to be able to override the specialValue for StringConversions and be true-ly static?