I have found several questions on the subject of the Scala compiler warning:
postfix operator xxx should be enabled by making the implicit value scala.language.postfixOps visible. This can be achieved by adding the import clause 'import scala.language.postfixOps' or by setting the compiler option -language:postfixOps. See the Scala docs for value scala.language.postfixOps for a discussion why the feature should be explicitly enabled.
Here's an example of where this warning shows up:
val m1 = List(1->"a",2->"b") toMap
None really answered what I wanted to know when I first came across this problem: how do I fix this problem without involving the import or the compiler option. My first thought was this: if it doesn't like postfix ops, then make the method/op call explicit by replacing the space with a dot. For example:
val m1 = List(1->"a",2->"b").toMap
Unfortunately, when I first tried that fix, the code that I happened to be working on at the time looked something like this:
val m2 = List(1->"a",2->"b") map {case (k,v) => (k.toString,v)} toMap
When I added the dot, this resulted in yet another obscure compiler error:
val m2 = List(1->"a",2->"b") map {case (k,v) => (k.toString,v)}.toMap
missing parameter type for expanded function The argument types of an anonymous function must be fully known. (SLS 8.5) Expected type was: ?
which related to the k and v identifiers.
Back then, I just gave up and added the import statement and all was well. However, a student recently asked me about this issue and I thought I'd better go back and investigate. See my answer to the problem which, now that I think about it, is rather obvious. Yet I hope to save some others the time that it takes to wade through all of the other discussions of this subject.
What I usually do in this situation, I'll add a dot to the first call:
val m2 = List(1->"a",2->"b").map {case (k,v) => (k.toString,v)}.toMap
Personally, I prefer this to having extra parenthesis.
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