关联参数化类型 [英] Relating parameterized types

查看:91
本文介绍了关联参数化类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个映射,其中键和值都是泛型类型。就像这样:

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

我想表达的是,对于每个键,类型 A 可能不同 - 值对,但每个键总是使用与其映射的值相同的类型进行参数化。因此, Foo [Int] 总是映射到 Bar [Int] ,a Foo [字符串] 总是映射到 Bar [字符串] 等等。



有人知道这种表达方式吗?

  trait参数//不重要它实际上做了什么

class示例{
val处理程序:Map [_<:Parameter,(_< ;: Parameter)=> _)= Map()

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




$ b $ p
$ b

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

事实证明,可以在Scala中定义一个异构地图。这里有一个粗略的草图:

  class HMap [A​​ [_],B [_]]扩展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迭代器:迭代器[HMap.Mapping [A,B,_]] =
新迭代器[HMap.Mapping [A,B,_]] {
val sub = self.iterator

def hasNext = sub.hasNext
def next():HMap.Mapping [A,B,_] = {
val( (键,值)= 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映射[A [_],B [_],T](val key:A [T],val value:B [T])
}

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



我的原创示例如下所示:

  object示例{
type Identity [T] = T
类型Handler [T] =(T)=> _
$ b $ val handlers = new HMap [Identity,Handler]

def doSomething(){
for(HMap.Mapping(value,handler)< - handlers ){
处理程序(值)
}
}
}

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

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.

解决方案

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.

这篇关于关联参数化类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆