关联参数化类型

发布于 2024-12-12 07:55:24 字数 672 浏览 0 评论 0原文

我有一个映射,其中键和值都是通用类型。像这样的事情:

Map[Foo[A], Bar[A]]

我想表达的是,映射中每个键值对的类型 A 可能不同,但每个键始终使用与值相同的类型进行参数化它映射到。因此 Foo[Int] 始终映射到 Bar[Int]Foo[String] 始终映射到 Bar[ String],等等。

有谁知道如何表达这个?

编辑:

这是我想做的事情的一个例子:

trait Parameter // not important what it actually does

class Example {
  val handlers: Map[_ <: Parameter, (_ <: Parameter) => _] = Map()

  def doSomething() {
    for ((value, handler) <- handlers) {
      handler(value)
    }
  }
}

这个想法是,一个值将始终映射到一个可以接受它作为参数的函数,但在编写代码时现在,编译器无法知道这一点。

I have a map where both the keys and values are generic types. Something like this:

Map[Foo[A], Bar[A]]

What I'd like to express is that the type A may be different for each key-value pair in the map, but every key is always parameterized with the same type as the value that it maps to. So a Foo[Int] always maps to a Bar[Int], a Foo[String] always maps to a Bar[String], and so forth.

Does anyone know a way to express this?

EDIT:

Here's an example of the sort of thing I'm trying to do:

trait Parameter // not important what it actually does

class Example {
  val handlers: Map[_ <: Parameter, (_ <: Parameter) => _] = Map()

  def doSomething() {
    for ((value, handler) <- handlers) {
      handler(value)
    }
  }
}

The idea is that a value will always map to a function that can accept it as a parameter, but as the code is written now, the compiler can't know this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

思慕 2024-12-19 07:55:24

事实证明,在 Scala 中定义异构映射是可能的。这是一个粗略的草图:

class HMap[A[_], B[_]] extends Iterable[HMap.Mapping[A, B, _]] {
  private val self = mutable.Map[A[_], B[_]]()

  def toMapping[T](a: A[_], b: B[_]): HMap.Mapping[A, B, T] = {
    HMap.Mapping(a.asInstanceOf[A[T]], b.asInstanceOf[B[T]])
  }

  def iterator: Iterator[HMap.Mapping[A, B, _]] =
    new Iterator[HMap.Mapping[A, B, _]] {
      val sub = self.iterator

      def hasNext = sub.hasNext
      def next(): HMap.Mapping[A, B, _] = {
        val (key, value) = sub.next()
        toMapping(key, value)
      }
    }

  def update[T](key: A[T], value: B[T]) = (self(key) = value)
  def get[T](key: A[T]) = self.get(key).asInstanceOf[Option[B[T]]]
  def apply[T](key: A[T]) = self(key).asInstanceOf[B[T]]
}

object HMap {
  case class Mapping[A[_], B[_], T](val key: A[T], val value: B[T])
}

这可以通过在内部使用映射的链接列表而不是映射来实现完全类型安全,但这对于性能更好。

我原来的示例如下所示:

object Example {
  type Identity[T] = T
  type Handler[T] = (T) => _

  val handlers = new HMap[Identity, Handler]

  def doSomething() {
    for (HMap.Mapping(value, handler) <- handlers) {
      handler(value)
    }
  }
}

这几乎是完美的,除了我不确定如何添加边界。

As it turns out, it is possible to define a heterogeneous map in Scala. Here's a rough sketch:

class HMap[A[_], B[_]] extends Iterable[HMap.Mapping[A, B, _]] {
  private val self = mutable.Map[A[_], B[_]]()

  def toMapping[T](a: A[_], b: B[_]): HMap.Mapping[A, B, T] = {
    HMap.Mapping(a.asInstanceOf[A[T]], b.asInstanceOf[B[T]])
  }

  def iterator: Iterator[HMap.Mapping[A, B, _]] =
    new Iterator[HMap.Mapping[A, B, _]] {
      val sub = self.iterator

      def hasNext = sub.hasNext
      def next(): HMap.Mapping[A, B, _] = {
        val (key, value) = sub.next()
        toMapping(key, value)
      }
    }

  def update[T](key: A[T], value: B[T]) = (self(key) = value)
  def get[T](key: A[T]) = self.get(key).asInstanceOf[Option[B[T]]]
  def apply[T](key: A[T]) = self(key).asInstanceOf[B[T]]
}

object HMap {
  case class Mapping[A[_], B[_], T](val key: A[T], val value: B[T])
}

This could be made completely typesafe by internally using a linked list of mappings instead of a map, but this is better for performance.

My original example would look like this:

object Example {
  type Identity[T] = T
  type Handler[T] = (T) => _

  val handlers = new HMap[Identity, Handler]

  def doSomething() {
    for (HMap.Mapping(value, handler) <- handlers) {
      handler(value)
    }
  }
}

This is almost perfect, except I'm not sure how to add bounds.

盛夏尉蓝 2024-12-19 07:55:24

您试图描述一种 更高等级多态,异构映射,映射中的每个键值对可以有不同的类型参数。尽管这很酷,但 Scala 的类型系统不允许您静态地表达这一点。我认为你能做的最好的事情就是定义一些可怕的、不安全的辅助方法:

def get [A] (map: Map[Foo[_], Bar[_]], k: Foo[A]) : Bar[A] 
def put [A] (map: Map[Foo[_], Bar[_]], k: Foo[A], v: Bar[A])

你可以使用 Manifest 在运行时具体化每个键值对的类型参数,从而使其更加安全,但我不知道如何...

You're trying to describe a sort of higher-rank polymorphic, heterogeneous map, where each key-value pair in the map can have a different type parameter. As cool as it would be, Scala's type system doesn't allow you to express this statically. I think the best you can do is to define some horrible, unsafe helper methods:

def get [A] (map: Map[Foo[_], Bar[_]], k: Foo[A]) : Bar[A] 
def put [A] (map: Map[Foo[_], Bar[_]], k: Foo[A], v: Bar[A])

You might be able to make it somewhat more safe using Manifests to reify the type parameters of each key-value pair at runtime, but I'm not sure how...

后eg是否自 2024-12-19 07:55:24

我已经实现了一个地图,可以满足您的需求。您可以在这里找到一些基本文档: https://github.com/sullivan-/emblem /wiki/TypeBoundMaps

TypeBoundMaps 采用具有单个类型参数的类型,因此您需要为映射中的键和值类型引入一些新类型:

trait Parameter
type Identity[P <: Parameter] = P
type Handler[P <: Parameter] = (P) => _

现在您可以像这样创建您想要的映射:

var handlers = TypeBoundMap[Parameter, Identity, Handler]()

下面是使用映射的几个示例:

trait P1 extends Parameter
trait P2 extends Parameter

val p1: P1 = new P1 {}
val f1: Handler[P1] = { p1: P1 => () }

handlers += p1 -> f1 // add a new pair to the map                                                             
val f2: Handler[P1] = handlers(p1) // retrieve a value from the map

现在,为了模仿示例中的 for 循环,我们需要引入一个新类型 TypeBoundPair,它是参数值匹配的键值对:

def handle[P <: Parameter](pair: TypeBoundPair[Parameter, Identity, Handler, P]): Unit = {
  pair._2(pair._1)
}

handlers.foreach { pair => handle(pair) }

引入 IdentityHandler 类型背后的想法在这里有更详细的解释:http://tinyurl.com/multi-tparam

I've implemented a map that does what you want. You can find some basic documentation on it here: https://github.com/sullivan-/emblem/wiki/TypeBoundMaps

TypeBoundMaps take types with a single type parameter, so you will need to introduce a couple of new types for your key and value types in the map:

trait Parameter
type Identity[P <: Parameter] = P
type Handler[P <: Parameter] = (P) => _

Now you can create the map you want like so:

var handlers = TypeBoundMap[Parameter, Identity, Handler]()

Here's a couple of examples of using the map:

trait P1 extends Parameter
trait P2 extends Parameter

val p1: P1 = new P1 {}
val f1: Handler[P1] = { p1: P1 => () }

handlers += p1 -> f1 // add a new pair to the map                                                             
val f2: Handler[P1] = handlers(p1) // retrieve a value from the map

Now, to imitate the for loop in your example, we need to bring in a new type TypeBoundPair, which is a key-value pair where the parameter values match:

def handle[P <: Parameter](pair: TypeBoundPair[Parameter, Identity, Handler, P]): Unit = {
  pair._2(pair._1)
}

handlers.foreach { pair => handle(pair) }

The idea behind introducing the Identity and Handler types is explained in more detail here: http://tinyurl.com/multi-tparam

吻泪 2024-12-19 07:55:24
scala> trait MyPair {
     | type T
     | val key:Foo[T]
     | val value:Bar[T]
     | }
defined trait MyPair

scala> var l:List[MyPair] = _
l: List[MyPair] = null

scala> l = List(new MyPair{type T = Int; val key = new Foo[Int]{}; val value = new Bar[Int]{} })
l: List[MyPair] = List($anon$1@176bf9e)

scala> l = List(new MyPair{type T = Int; val key = new Foo[Int]{}; val value = new Bar[Int]{} }, new MyPair {type T = String; val key = new Foo[String]{}; val value = new Bar[String]{} })
l: List[MyPair] = List($anon$1@d78fb4, $anon$4@1b72da)

scala> l = List(new MyPair{type T = Int; val key = new Foo[Int]{}; val value = new Bar[Int]{} }, new MyPair {type T = String; val key = new Foo[String]{}; val value = new Bar[Int]{} })
<console>:11: error: overriding value value in trait MyPair of type Bar[this.T];
 value value has incompatible type
       l = List(new MyPair{type T = Int; val key = new Foo[Int]{}; val value = new Bar[Int]{} }, new MyPair {type T = String; val key = new Foo[String]{}; val value = new Bar[Int]{} })
scala> trait MyPair {
     | type T
     | val key:Foo[T]
     | val value:Bar[T]
     | }
defined trait MyPair

scala> var l:List[MyPair] = _
l: List[MyPair] = null

scala> l = List(new MyPair{type T = Int; val key = new Foo[Int]{}; val value = new Bar[Int]{} })
l: List[MyPair] = List($anon$1@176bf9e)

scala> l = List(new MyPair{type T = Int; val key = new Foo[Int]{}; val value = new Bar[Int]{} }, new MyPair {type T = String; val key = new Foo[String]{}; val value = new Bar[String]{} })
l: List[MyPair] = List($anon$1@d78fb4, $anon$4@1b72da)

scala> l = List(new MyPair{type T = Int; val key = new Foo[Int]{}; val value = new Bar[Int]{} }, new MyPair {type T = String; val key = new Foo[String]{}; val value = new Bar[Int]{} })
<console>:11: error: overriding value value in trait MyPair of type Bar[this.T];
 value value has incompatible type
       l = List(new MyPair{type T = Int; val key = new Foo[Int]{}; val value = new Bar[Int]{} }, new MyPair {type T = String; val key = new Foo[String]{}; val value = new Bar[Int]{} })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文