scala在语法上规范我们用表达式去思维,表达式就是数学中有符号和操作数组成的式子

scala 调用函数的语法不能用一两句话说明白,这里只给出些例子。


这样都可以

package testscala

import scala.io.Source
class A {
  def f():Unit = println("hello")
}
object Test extends App {
  val a = new A
  a f  
  
  a.f  
  
  a.f()
  
  a f()
}


这样不可以

package testscala

import scala.io.Source
class A {
  def f():Unit = println("hello")
}
object Test extends App {
  val a = new A
  a f  
  a.f  // 这一行被当作了上一行的参数, 所以这样使用不安全
  a.f()
  a f()
}


这样不可以

package testscala

import scala.io.Source
class A {
  def f:Unit = println("hello")
}
object Test extends App {
  val a = new A
  a.f()  // error
  a f()  // error
}


这样是可以的

package testscala

object Test extends App {
  val a = List(1, 2, 3)
  a map 
  
}


一般来讲:当没有参数的时候,只有一个参数的0阶函数时候,是可以省略括号的

0阶函数:不是高阶函数的函数都叫0阶函数


顺便复习一下不带括号的方法,和带有一个空括号的方法有什么区别

To summarize, it is encouraged style in Scala to define methods that take no parameters and have no side effects as parameterless methods, i.e., leaving off the empty parentheses. On the other hand, you should never define a method that has side-effects without parentheses, because then invocations of that method would look like a field selection

能不带括号就不带括号,除非你想提醒自己这个方法有副作用。

如果定义的时候没有带括号,调用的时候是不能带括号的

如果定义的时候带括号,调用的时候可以带括号也可以不带括号