泛型方法上限为另一个泛型类型时,无法以泛型类型作为参数。

3

我正在尝试创建一个通用工厂方法,它具有接收该通用类型参数并返回该通用类型结果的方法的上限。我尝试了以下代码:

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

有没有办法让这个工作?
1个回答

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

抱歉我表达不够清晰。假设我将一个整数传递给apply函数。我希望有一个方法,它接受一个整数并返回一个整数。我不想让类型定义一个通用的方法 - sublixt

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接