I can name objects like this, but can't call m
:
object + {
def m (s: String) = println(s)
}
Can't call +.m("hi")
:
<console>:1: error: illegal start of simple expression
+.m("hi")
Also can't call + m "hi"
(preferred for DSL-usage).
But with object ++
it works fine! Do they conflict with (not existent) unary_+
methods? Is it possible to avoid this?
I believe the problem is that in order to handle unary operators without ambiguity, scala relies on a special case: only
!
,+
,-
and~
are treated as unary operators. Thus in+.m("hi")
, scala treat+
as an unary operator and can't make sense of the whole expression.Another code using package:
java version "1.7.0_09"
Scala code runner version 2.9.2
Indeed it is not possible with unary operators. If you want to call it anyways, you could resort to using the name generated by the compiler for the JVM (which starts with a dollar):