gpt4 book ai didi

scala - 通用值类

转载 作者:行者123 更新时间:2023-12-02 08:10:28 25 4
gpt4 key购买 nike

是否可以在 scala 中为某些 Numeric[T] 定义值类?我试过这样的事情:

case class Inches[T <: Numeric[T]](value: T)(implicit num: Numeric[T]) extends AnyVal

但是我得到编译错误,value classes can only a parameter

有什么办法可以绕过这个吗?

谢谢。

最佳答案

您可以移动 Numeric从类到需要它的方法的参数:

case class Inches[T](value: T) extends AnyVal {
def foo()(implicit num: Numeric[T]) = ...
}

(T <: Numeric[T] 是错误的。)

关于scala - 通用值类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47604342/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com