How to unimport String “+” operator in Scala?

后端 未结 3 1436
别跟我提以往
别跟我提以往 2020-12-06 16:41

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 conv

相关标签:
3条回答
  • 2020-12-06 17:17

    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 &?

    0 讨论(0)
  • 2020-12-06 17:17

    You can't unimport it, but you could use +: and define it on the int class. What would be best is if you write it like this: "2".toInt + 3.

    0 讨论(0)
  • 2020-12-06 17:19

    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.

    0 讨论(0)
提交回复
热议问题