I'm writing a DSL where the "+" operator is strictly numeric, like some other popular languages. It's close, but the String "+" operator is messing up my implicit conversions. What's the syntax for unimporting an operator of the String class?
Just to be clearer, instead of this:
scala> var x = "2" + 3; x: java.lang.String = 23
I'd like to get x: Int = 5
I imagine I just need 2 things to make that happen:
I'm stuck on the first step.
Thanks
Creating a Stringvar greeting = "Hello world!"; or var greeting:String = "Hello world!"; Whenever compiler encounters a string literal in the code, it creates a String object with its value, in this case, “Hello world!”.
Scala string is an immutable object that means the object cannot be modified. Each element of a string is associated with an index number. The first character is associated with the number 0, the second with the number 1, etc. Class java. lang.
Syntax. var myVar = 10; val myVal = "Hello, Scala!"; Here, by default, myVar will be Int type and myVal will become String type variable.
Console. readChar , aliased in in current incarnations of Scala as Predef. getChar is the most direct way. scala> readChar res0: Char = !
According to section 12.3.1 of the Scala spec, the +
method for String
has special treatment by the compiler. I don't know for sure, but I think this means you can't "unimport" it, which is a shame because it really breaks the type system (much like the related toString
method).
Could you use a different name for the operator in your DSL, eg, ++
or &
?
The + method for a string is a method on of the String class (and therefore on each string object), and as such it cannot be unimported.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With