如何取消导入字符串“+"Scala 中的运算符? [英] How to unimport String "+" operator in Scala?
问题描述
我正在编写一个 DSL,其中+"运算符是严格的数字,就像其他一些流行语言一样.它很接近,但是字符串+"运算符弄乱了我的隐式转换.取消导入 String 类的运算符的语法是什么?
为了更清楚,而不是这样:
<前>Scala> var x = "2" + 3;x: java.lang.String = 23我想得到 x: Int = 5
我想我只需要两件事就可以做到这一点:
- 从字符串中删除(在我的范围内取消导入)+"的定义
- 定义 String 到 Int 的隐式转换
我被困在第一步.
谢谢
根据 Scala 规范,String
的 +
方法被编译器特殊处理.我不确定,但我认为这意味着您不能取消导入"它,这是一种耻辱,因为它确实破坏了类型系统(很像相关的 toString
方法).
您能否在 DSL 中为运算符使用不同的名称,例如 ++
或 &
?
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:
- Remove (unimport within my scope) the definition of "+" from Strings
- Define an implicit conversion of String to Int
I'm stuck on the first step.
Thanks
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 &
?
这篇关于如何取消导入字符串“+"Scala 中的运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!