带类型参数的函数中调用的Typeclasses方法 [英] Typeclasses methods called in functions with type parameters
本文介绍了带类型参数的函数中调用的Typeclasses方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在带有类型参数的函数中使用typeclass方法.不幸的是我无法正常工作.
I'm trying to use a typeclass method inside a function with a type parameter. Unfortunately I can't get it to work.
当我尝试使用方法 addRegisterInOut 时,编译器将在以下代码上引发错误.
When I try to use the method addRegisterInOut the compiler throws an error on the following code.
class Module extends Component {
// ...
trait HasDataSpecs[T] {
def dataSpecs(datatype: T) : RegisterSpecs
}
implicit object SpecsBool extends HasDataSpecs[Bool] {
override def dataSpecs(datatype: Bool) : RegisterSpecs = {
new RegisterSpecs("", 1)}
}
implicit object SpecsUInt extends HasDataSpecs[UInt] {
override def dataSpecs(datatype: UInt) : RegisterSpecs = {
new RegisterSpecs("", datatype.getBitsWidth)}
}
def dataSpecs[A](a: A)(implicit ds: HasDataSpecs[A]) : RegisterSpecs = ds.dataSpecs(a)
// ...
def addRegisterInOut[T <: Data](name: String, out_data: T) : T = {
var specs = dataSpecs(out_data)
specs.name = name
specs.permission = Permission.RWPerm
registers += specs
out_len += out_data.getBitsWidth
in_len += out_data.getBitsWidth
val in_data = cloneOf(out_data)
in_channels += in_data
out_channels += out_data
in_data
}
}
//... Somewhere I use addRegisterInOut(UInt(...))
我知道
[error] /home/bellandi/Projects/VHDL/spinalhdl_experiments/src/main/scala/spinal/msk/lib/pkg/RegisterCls.scala:83:26: could not find implicit value for parameter ds: Module.this.HasDataSpecs[T]
[error] var specs = dataSpecs(out_data)
[error] ^
我希望编译器仅在未定义隐式的类型上暂停,但似乎总是发生.
I would expect the compiler to halt only on types where an implicit is not defined, but it seems to happen always.
推荐答案
尝试添加 查看全文