String
is a final class in the JDK for lots of reasons - basically you could compromise the entire security model of the JVM if you were allowed to fiddle with its equals
method! (See this question for detailed reasons.)
That being so, the best you will be able to do in your DSL is introduce a new operator that is not defined for String
, for example ===
, and make sure you have an implicit conversion available for String
to BString
:
class BString(val string: String) {
def ===(other: BString) = TODO
}
object BString {
implicit def string2BString(x: String) = new BString(x)
}
You might also consider making your BString
class final as well. Writing a correct equals
method can be difficult or impossible in the face of inheritance - think about the asymmetry that you have already witnessed when trying to compare String
and Object
, and check out this old article for a thorough treatment of the problem.