标签:
There are two standard ways of calling methods:
obj.method(params) // dot notation obj method (params) // operator notation
The above can be modified in the following ways:
There‘s also some stuff with tuple inference, but I try to avoid it, so I‘m not sure of the exact rules.
None of these will explain the example you are confused about, however. Before I explain it, however, I‘d like to show some syntactic sugars that can also be used to call methods:
obj(params) // equivalent to obj.apply(params) obj.x = y // equivalent to obj.x_=(y), if obj.x also exists obj(x) = y // equivalent to obj.update(x, y) obj op= y // equivalent to obj = obj op y, if op is symbolic ~obj // equivalent to obj.unary_~; also for !, + and -, but no other symbol
Ok, now to the example you gave. One can import members of stable values. Java can do it for static methods with its static import, but Scala has a more general mechanism: importing from packages, objects or common instances is no different: it brings both type members and value members. Methods fall in the latter category.
So, imagine you have val a = 2, and you do import a._. That will bring into scope all of Intmethods, so you can call them directly. You can‘t do +(2), because that would be interpreted as a call to unary_+, but you could call *(4), for example:
scala> val a = 2 a: Int = 2 scala> import a._ import a._ scala> *(4) res16: Int = 8
Now, here‘s the rule. You can call
method(params)
If:
Note that there‘s a precedence issue as well. If you write obj method(params), Scala will presume method belongs to obj, even if it was imported into scope.
标签:
原文地址:http://my.oschina.net/Barudisshu/blog/416408