【问题标题】:Generic with method as upper bound cannot take generic type as parameter以方法为上限的泛型不能将泛型类型作为参数
【发布时间】:2012-11-23 15:11:00
【问题描述】:

我正在尝试创建一个泛型工厂方法,该方法具有一个方法的上限,该方法采用该泛型类型的参数并返回该泛型类型的某些内容。我试过了,

def apply[Type <: {def *(that: Type): Type}](length: Int)(implicit manifest: Manifest[Type]) = new Array[Type](length)

但我收到此错误,

Parameter type in structural refinement may not refer to an abstract type defined outside that refinement

有没有办法让这个工作?

【问题讨论】:

    标签: scala generics


    【解决方案1】:

    根据该错误消息,您应该将* 定义为这样的通用函数: def apply[Type &lt;: {def *[Type](that: Type): Type}](length: Int)(implicit manifest: Manifest[Type]) = new Array[Type](length)

    【讨论】:

    • 对不起,我不是很清楚。假设我将一个 int 传递给 apply 函数。我希望有一个方法 * 接受一个 int 并返回一个 int。我不希望该类型定义泛型方法 *.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-08
    • 1970-01-01
    • 1970-01-01
    • 2019-03-05
    相关资源
    最近更新 更多