相关疑难解决方法(0)

Scala继承参数化构造函数

我有一个带有几个可选参数的抽象基类:

abstract case class Hypothesis(
    requirement: Boolean = false,
    onlyDays:   Seq[Int] = Nil,
    …
) extends Something {…}
Run Code Online (Sandbox Code Playgroud)

我是否真的需要在顶部使用附加关键字override val明确重复所有参数‽

case class SomeHypothesis(
    anotherArg: SomeType,
    override val requirement: Boolean = false,
    override val onlyDays:   Seq[Int] = Nil,
    …
) extends Hypothesis(
    requirement,
    onlyDays,
    …
) {…}
Run Code Online (Sandbox Code Playgroud)

或者是否有类似的语法

case class SomeHypothesis(anotherArg: SomeType, **) extends Hypothesis(**) {…}
Run Code Online (Sandbox Code Playgroud)

我甚至不需要anotherArg,只是将所有关键字args传递给超级构造函数的方法.


我真的很喜欢Scala关于构造函数的想法,但是如果没有这个的语法,我会很失望的:(

inheritance constructor scala

24
推荐指数
2
解决办法
2万
查看次数

为什么不能隐含类的第一个参数列表?

scala> class A(implicit a: Int);
defined class A

scala> class B()(implicit a: Int);
defined class B

scala> new A()(1)
res1: A = A@159d450

scala> new B()(1)
res2: B = B@171f735

scala> new A(1)
<console>:7: error: too many arguments for constructor A: ()(implicit a: Int)A
       new A(1)
Run Code Online (Sandbox Code Playgroud)

为什么Scalac在类声明中提供的隐式参数列表之前插入一个空参数列表?

从scalac来源评论来看,这似乎是一个特征,而不是一个错误:

//如果它是唯一的参数部分,则将(隐式...)转换为()(隐式...)

我很想知道为什么这样做.我觉得这很令人惊讶.

constructor scala implicit

18
推荐指数
2
解决办法
1257
查看次数

标签 统计

constructor ×2

scala ×2

implicit ×1

inheritance ×1