Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 将类型变量绑定到集合的元素类型_Scala_Higher Kinded Types - Fatal编程技术网

Scala 将类型变量绑定到集合的元素类型

Scala 将类型变量绑定到集合的元素类型,scala,higher-kinded-types,Scala,Higher Kinded Types,如何将多态类型变量绑定到Scala中一元类型构造函数的参数 def f[CollectionOfE] = new Blah[...] { def g(a: E) = { ... } } ... val x = f[Set[Int]] // want E above to bind to Int 在g的定义中,我希望能够引用已实例化f的集合的参数类型 我试过: def f[C[E]] = new Blah[...] ... 但是E的范围似乎是[..]的局部范围,如果这有意义的

如何将多态类型变量绑定到Scala中一元类型构造函数的参数

def f[CollectionOfE] = new Blah[...]
{
  def g(a: E) = 
  { ... } 
}

...

val x = f[Set[Int]]  // want E above to bind to Int
在g的定义中,我希望能够引用已实例化f的集合的参数类型

我试过:

def f[C[E]] = new Blah[...] ...

但是E的范围似乎是
[
..
]
的局部范围,如果这有意义的话…

如果您定义了一个单独的参数,您可以这样做。例如

def f[E, C <: util.Collection[E]] = new Blah  {
   def g(a: E) = ...
}

val x = f[Int, Set[Int]].g(1)  // compiles

val y = f[Int, Set[Int]].g("string")  // doesn't compile

如果我正确理解你的意图,你可能会想要这样的东西:

def f[E, C[_]] = new Blah[...] {
  def g(e: E) = ???
}

...

f[Int, Set]
基本上,如果以后要引用类型,需要将其作为单独的类型参数。您可能还希望将
C[\u]
的类型限制为某些集合

def f[E, C[_]] = new Blah[...] {
  def g(e: E) = ???
}

...

f[Int, Set]