Scala method call syntax

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:

  1. If params is a single parameter, you can replace () with {}.
  2. If params is a single parameter and you are using operator notation, you can drop the parenthesis.
  3. If method doesn‘t take parameters, you can drop (params) (that is, drop the empty ()).
  4. If method ends with :, then it actually binds to the right in operator notation. That is, (params) method_: obj is equivalent to obj.method_:(params).
  5. Either way, spaces are optional as long as identifiers can be told apart. So one can add spaces to the dot notation, like obj . method ( params ) or write .method(params) on the next line -- as often happens with call chaining --, as well as remove spaces from the operator notation, as ina+b.

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:

  1. method was imported into scope.
  2. You keep the parenthesis (even if there‘s only one parameter)

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.

时间: 2024-11-05 23:20:20

Scala method call syntax的相关文章

scala method

刚接触scala,做练习的时候碰到一个问题,顺便mark一下. 先看下面一段代码: 1 def sum(args:Int*) = { 2 var result = 0 3 for (arg <- args) 4 result += arg 5 result 6 } 7 8 object ScalaApp { 9 def main(args: Array[String]): Unit = { 10 val s = sum(1, 4, 9, 16, 25) 11 println(s) 12 } 13

Beginning Scala study note(4) Functional Programming in Scala

1. Functional programming treats computation as the evaluation of mathematical and avoids state and mutable data. Scala encourages an expression-oriented programming(EOP) 1) In expression-oriented programming every statement is an expression. A state

Beginning Scala study note(9) Scala and Java Interoperability

1. Translating Java Classes to Scala Classes Example 1: # a class declaration in Java public class Book{} # Scala equivalent of a class declaration class Book Example 2: # a Java class with a Construtor public class Book{ private final int isbn; priv

Beginning Scala study note(2) Basics of Scala

1. Variables (1) Three ways to define variables: 1) val refers to define an immutable variable; scala> val x = 10 x: Int = 10 scala> x*x res4: Int = 100 scala> res4 + 1 # use result as a value res6: Int = 101 scala> res4 + res6 res7: Int = 201

Item 20: Use call to Call Methods with a Custom

Item 20: Use call to Call Methods with a CustomReceiverOrdinarily, the receiver of a function or method (i.e., the value boundto the special keyword this ) is determined by the syntax of its caller.In particular, the method call syntax binds the obje

Object Oriented Programming python

new concepts of the object oriented programming : class encapsulation inheritance polymorphism the three features of an object are : identity, state and behaviora class is an abstraction which regroup objects who have the same attributes and the same

sparkstreaming源码分析

做个笔记,记录streaming任务执行的整个流程,下文使用的源码是master分支的代码,1.2.1版本已经发布,应该和1.2.1差别不大 1.streaming程序是从StreamingContext.start()开始的,做一个必要的参数检查然后启动 jobscheduler StreamingContext.scala def start(): Unit = synchronized {     if (state == Started) {       throw new SparkE

CAF(C++ actor framework)(序列化之类,无需序列化,直接传)(二)

昨天讲了Struct,还是不够满意,毕竟C++里面类用的比较多嘛,那就先上个类,这段代码是我稍微改编了一下的结果. #include <utility> #include <iostream> #include <vector> #include "caf/all.hpp" using std::cout; using std::endl; using std::make_pair; using std::vector; using namespac

haproxy实现动静分离机制

ACL的简介: haproxy的ACL用于实现基于请求报文的首部.响应报文的内容或其它的环境状态信息来做出转发决策,这大大增强了其配置弹性.其配置法则通常分为两步,首先去定义ACL,即定义一个测试条件,而后在条件得到满足时执行某特定的动作,如阻止请求或转发至某特定的后端. syntax: acl <aclname> <criterion> [flags] [operator] <value> ... Description: <aclname>:ACL名称,