在Scala中何时可以安全地省略括号?

在Scala中何时可以安全地省略括号?,scala,syntax,Scala,Syntax,以下是一个玩具示例: object Example { import collection.mutable import collection.immutable.TreeSet val x = TreeSet(1, 5, 8, 12) val y = mutable.Set.empty ++= x val z = TreeSet.empty ++ y // This gives an error: unspecified parameter // val z

以下是一个玩具示例:

object Example {

  import collection.mutable
  import collection.immutable.TreeSet

  val x = TreeSet(1, 5, 8, 12)
  val y = mutable.Set.empty ++= x
  val z = TreeSet.empty ++ y
  // This gives an error: unspecified parameter
  //  val z = TreeSet.empty() ++ y

}
显然
TreeSet.empty
TreeSet.empty()
不是一回事。引擎盖下面发生了什么?我什么时候可以安全地省略(或者在本例中不省略)括号


使现代化 我已经向控制台发送了一些代码,然后在评估上述代码之前在intellij中将其删除,如下所示:

  implicit object StringOrdering extends Ordering[String] {
    def compare(o1: String, o2: String) = {
      o1.length - o2.length
    }
  }
  object StringOrdering1 extends Ordering[String] {
    def compare(o1: String, o2: String) = {
      o2.length - o1.length
    }
  }

这是一种特殊情况,与何时可以省略括号和何时不能省略括号无关

这是树集的签名。空的:

def empty[A](implicit ordering: Ordering[A]): TreeSet[A]
它有一个隐式参数列表,需要对包含的类型
A
进行
排序。调用
TreeSet.empty
时,编译器将尝试隐式查找正确的
排序[A]

但是当您调用
TreeSet.empty()
时,编译器认为您试图显式提供隐式参数。除非您在列表中遗漏了参数,这是一个编译错误(参数数量错误)。唯一可行的方法是显式传递一些
排序
TreeSet.empty(Ordering.Int)



旁注:上面的代码实际上并不是使用
TreeSet.empty
编译的,因为它会导致
排序的模糊隐式错误。在您的范围中可能有一些隐含的
排序[Int]
,您没有将其包括在问题中。最好将类型显式化,并使用
TreeSet.empty[Int]

您使用的是什么版本的Scala
TreeSet.empty
对我的含蓄发散表示不满。我正在使用Scala 2.11.7