小编All*_*hou的帖子

Scala方法类型参数不能接受forSome形式的存在类型

在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)

types scala existential-type

2
推荐指数
1
解决办法
225
查看次数

标签 统计

existential-type ×1

scala ×1

types ×1