多个构造函数和隐式参数
Multiple constructor and implicit parameter
我有一些 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)
}
...
}
编译器抛出 "could not find implicit value for parameter" 错误。我试过 this(a,0)(typeinfo)
但得到了同样的错误
可能是什么原因?
主构造函数是您在 class 声明中定义的构造函数
this
是二级构造函数,还是函数,需要在声明中定义隐式。
如果你这样做:
class A (a: Int, b:Int) (implicit typeinfo: TypeInformation[T]) {
def this(a: Int])(implicit typeinfo: TypeInformation[T]) {
this(a, 0)
}
...
}
它会起作用。
我有一些 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)
}
...
}
编译器抛出 "could not find implicit value for parameter" 错误。我试过 this(a,0)(typeinfo)
但得到了同样的错误
可能是什么原因?
主构造函数是您在 class 声明中定义的构造函数
this
是二级构造函数,还是函数,需要在声明中定义隐式。
如果你这样做:
class A (a: Int, b:Int) (implicit typeinfo: TypeInformation[T]) {
def this(a: Int])(implicit typeinfo: TypeInformation[T]) {
this(a, 0)
}
...
}
它会起作用。