Pri*_*osK 6 parameters constructor scala
我希望对此声明之间的差异进行解释:
class Clazz(param1: String, param2: Integer)
Run Code Online (Sandbox Code Playgroud)
还有这个:
class Clazz(param1: String)(param2: Integer)
Run Code Online (Sandbox Code Playgroud)
第二个声明是否只影响实例化对象的方式,还是有更深层次的原因我不知道.
我想到的一个原因是多个可变长度的参数,例如:
class Clazz(param1: String*)(param2: Integer*)
Run Code Online (Sandbox Code Playgroud)
还有其他人吗?
mis*_*tor 11
#1类型推断.它从左到右,按参数列表完成.
scala> class Foo[A](x: A, y: A => Unit)
defined class Foo
scala> new Foo(2, x => println(x))
<console>:24: error: missing parameter type
new Foo(2, x => println(x))
^
scala> class Foo[A](x: A)(y: A => Unit)
defined class Foo
scala> new Foo(2)(x => println(x))
res22: Foo[Int] = Foo@4dc1e4
Run Code Online (Sandbox Code Playgroud)
#2隐式参数列表.
scala> class Foo[A](x: A)(implicit ord: scala.Ordering[A]) {
| def compare(y: A) = ord.compare(x, y)
| }
defined class Foo
scala> new Foo(3)
res23: Foo[Int] = Foo@965701
scala> res23 compare 7
res24: Int = -1
scala> new Foo(new {})
<console>:24: error: No implicit Ordering defined for java.lang.Object.
new Foo(new {})
^
Run Code Online (Sandbox Code Playgroud)