scala - 多个构造函数和隐式参数
问题描述
我有一些scala代码
class A (a: Int, b:Int) (implicit typeinfo: TypeInformation[T]) {
...
}
但是如果我定义一个新的构造函数
class A (a: Int, b:Int) (implicit typeinfo: TypeInformation[T]) {
def this(a: Int]) {
this(a, 0)
}
...
}
编译器抛出“找不到参数的隐式值”错误。我试过this(a,0)(typeinfo)
但得到了同样的错误
可能是什么原因?
解决方案
主构造函数是您在类声明中定义的构造函数
this
是一个辅助构造函数,它仍然是一个函数,你需要在声明中定义隐式。
如果你这样做:
class A (a: Int, b:Int) (implicit typeinfo: TypeInformation[T]) {
def this(a: Int])(implicit typeinfo: TypeInformation[T]) {
this(a, 0)
}
...
}
它会起作用的。
推荐阅读
- reactjs - Gatsby 不渲染组件
- c# - Unity:使用不同的 KeyPress 跳过游戏对象
- reactjs - 如何从地图访问对象?
- r - 有没有办法从 R 中的字符串间隔值中获取最小值或最大值?
- reactjs - 未安装组件上的组件内部反应状态更新
- reactjs - 在可折叠表reactjs中使用地图
- swift - 使用 Carousel UIScrollView 在两个视图之间滚动的 SwiftUI
- reactjs - ESLint - 渲染 $ 时出现“react/jsx-one-expression-per-line”错误
- composer-php - drupal/core-composer-scaffold 不会将核心放在 web 文件夹中
- numpy - float 和 numpy.ufunc 不支持的操作数类型