Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
想象一下,我有以下类定义:
class Foo[T]
我想做以下事情
def bar(x:Foo[ =>Int ]):Int = ???
但编译器因“此处不允许使用名称参数类型”而失败
如何使用按名称类型作为泛型方法的类型参数?
您必须提供自己的惰性包装器。像这样的东西:
class Lazy[T](wrp: => T) { lazy val value: T = wrp }
进而:
def bar(x: Foo[Lazy[T]]): Int = ???