scala - 在 scala 中绑定的类型之后的另一个子类型
问题描述
class PEControl[T <: Data : Arithmetic](accType: T)
,这是来自 riscv-gemmini 的类定义。Data
type 是 chisel 中的基本数据类型,提供对, 和Arithmetic
的一些算术运算。Data
abstract class Arithmetic[T <: Data]
使用的语法是<: Type : Type
什么,这是什么意思?我发现语法是TypeParamBounds ::= TypeBounds {‘:’ Type}
从这里调用的。哪里可以详细了解一下,谢谢。
解决方案
类型边界是以下的简写:
class PEControl[T <: Data : Arithmetic](accType: T)
// equivalent to
class PEControl[T <: Data](accType: T)(implicit noName: Arithmetic[T])
// which means in both cases in the body of your class
// you can summon instances of arithmetic for all Ts
class PEControl[T <: Data : Arithmetic](accType: T) {
def doSomethingWithT(t1: T, t2: T): Unit = {
implicitly[Arithmetic[T]].plus(t1, t2)
}
}
推荐阅读
- amazon-web-services - 无法安装/使用 AWS 工具 Powershell cmdlet
- php - 在 PHP 中排队作业
- python - 在 VPS 上使用 python 套接字的 winerror 10061
- powershell - 简单查询在 Powershell_ISE 中不起作用,但在 Powershell 上不起作用
- javascript - 文档的 Firestore 数据聚合
- python - 使用 pool.map 进行多处理时无法腌制本地对象
- excel - Excel计算最高颜色值
- asp.net-core - Asp net core env vars不适用于array-within-array appsettings Serilog属性
- php - 运行 docker-php-ext-install pdo mcrypt mbstring 时出错
- python - 当 ORTOOLS 提出解决方案时评估自定义函数