关联参数化类型
我有一个映射,其中键和值都是通用类型。像这样的事情:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
事实证明,在 Scala 中定义异构映射是可能的。这是一个粗略的草图:
这可以通过在内部使用映射的链接列表而不是映射来实现完全类型安全,但这对于性能更好。
我原来的示例如下所示:
这几乎是完美的,除了我不确定如何添加边界。
As it turns out, it is possible to define a heterogeneous map in Scala. Here's a rough sketch:
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:
This is almost perfect, except I'm not sure how to add bounds.
您试图描述一种 更高等级多态,异构映射,映射中的每个键值对可以有不同的类型参数。尽管这很酷,但 Scala 的类型系统不允许您静态地表达这一点。我认为你能做的最好的事情就是定义一些可怕的、不安全的辅助方法:
你可以使用
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:
You might be able to make it somewhat more safe using
Manifest
s to reify the type parameters of each key-value pair at runtime, but I'm not sure how...我已经实现了一个地图,可以满足您的需求。您可以在这里找到一些基本文档: https://github.com/sullivan-/emblem /wiki/TypeBoundMaps
TypeBoundMaps 采用具有单个类型参数的类型,因此您需要为映射中的键和值类型引入一些新类型:
现在您可以像这样创建您想要的映射:
下面是使用映射的几个示例:
现在,为了模仿示例中的 for 循环,我们需要引入一个新类型
TypeBoundPair
,它是参数值匹配的键值对:引入
Identity
和Handler
类型背后的想法在这里有更详细的解释:http://tinyurl.com/multi-tparamI'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:
Now you can create the map you want like so:
Here's a couple of examples of using 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:The idea behind introducing the
Identity
andHandler
types is explained in more detail here: http://tinyurl.com/multi-tparam