Scala 集合类型之间的隐式转换
我想在 Scala XML Elem 对象和 XML 元素的另一种表示形式(在我的例子中为 dom4j 元素)之间隐式转换。我编写了以下隐式转换:
implicit def elemToElement(e: Elem): Element = ... do conversion here ...
implicit def elementToElem(e: Element): Elem = ... do conversion here ...
到目前为止,效果很好。
现在我还需要所述元素的集合来进行双向转换。首先,我是否绝对需要编写额外的转换方法?如果我不这样做,事情似乎就不会成功。
我尝试编写以下内容:
implicit def elemTToElementT(t: Traversable[Elem]) = t map (elemToElement(_))
implicit def elementTToElemT(t: Traversable[Element]) = t map (elementToElem(_))
这看起来不太理想,因为如果转换方法采用 Traversable,那么它也会返回 Traversable。如果我传递一个 List,我也会得到一个 Traversable。所以我认为应该以某种方式对转换进行参数化。
那么编写这些转换以尽可能通用的标准方法是什么?
I would like to implicitly convert between the Scala XML Elem object and another representation of an XML element, in my case dom4j Element. I wrote the following implicit conversions:
implicit def elemToElement(e: Elem): Element = ... do conversion here ...
implicit def elementToElem(e: Element): Elem = ... do conversion here ...
So far so good, this works.
Now I also need collections of said elements to convert both ways. First, do I absolutely need to write additional conversion methods? Things didn't seem to work if I didn't.
I tried to write the following:
implicit def elemTToElementT(t: Traversable[Elem]) = t map (elemToElement(_))
implicit def elementTToElemT(t: Traversable[Element]) = t map (elementToElem(_))
This doesn't look too ideal because if the conversion method takes a Traversable, then it also returns a Traversable. If I pass a List, I also get a Traversable out. So I assume the conversion should be parametrized somehow.
So what's the standard way of writing these conversions in order to be as generic as possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这并不简单,所以为了得到你想要的东西,我认为你必须更深入一点。本文详细解释了 scala 集合的工作原理(读起来也很有趣):http ://www.artima.com/scalazine/articles/scala_collections_architecture.html
您基本上是在尝试执行与 List.map(...) (或具有实现的 TraversableLike )和类似方法相同的操作。 ...仅另外进行隐式转换。
更新:
我开始对此进行一些尝试,并根据 TraversableLike.map(...) 的功能编写了一个转换。然而我发现即使没有它它也能工作。看起来 Scala 开箱即用地支持它(至少在我的机器上:-)):
这就是你想要的?
This is non-trivial so in order get what you want I think you'll have to go a bit deeper. This article explains a lot about how the scala collections work (and interesting read it is too): http://www.artima.com/scalazine/articles/scala_collections_architecture.html
You're basically trying to do the same as List.map(...) (or TraversableLike which has the implementation) and similar methods.... only with implicit conversion in addition.
Update:
I started experimenting a little bit with this and wrote a conversion based on what TraversableLike.map(...) does. However I discovered that it worked even without it. Seems like Scala supports it out of the box (at least on my machine :-) ):
This was what you were after?
我认为这可能太过于隐含了。特别是当您可以在地图中使用转换器方法时,
我认为您想要的级别简洁性会变得混乱。我将创建一个转换器层并仅在其中一种表示中工作,以免事情变得混乱。
I think this might be taking implicits too far. Especially as you can just use the converter method in a map
I think that the level conciseness you're going for is getting into obfuscation. I would create a converter layer and work in only one of the representations so as to keep things from getting to confused.