在Scala中,存在类型有以下两种形式:
// placeholder syntax
List[_]
// forSome
List[T forSome {type T}]
Run Code Online (Sandbox Code Playgroud)
但是,似乎第二种形式不能出现在方法类型参数位置(至少在我写下面的方式).
// placeholder syntax is Okay
scala> def foo[List[_]](x: List[_]) = x
foo: [List[_]](x: List[_])List[_]
scala> def foo[List[t forSome {type t}]](x: List[_]) = x
<console>:1: error: ']' expected but 'forSome' found.
def foo[List[T forSome {type T}]](x: List[_]) = x
^
// being as upper bound is also Okay
scala> def foo[A <: List[T forSome { type T }]](x: A) = x
foo: [A <: List[T forSome { type T …Run Code Online (Sandbox Code Playgroud)