Scala 集合类型之间的隐式转换

发布于 2024-10-11 14:23:26 字数 693 浏览 8 评论 0原文

我想在 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 技术交流群。

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

发布评论

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

评论(2

日久见人心 2024-10-18 14:23:26

这并不简单,所以为了得到你想要的东西,我认为你必须更深入一点。本文详细解释了 scala 集合的工作原理(读起来也很有趣):http ://www.artima.com/scalazine/articles/scala_collections_architecture.html

您基本上是在尝试执行与 List.map(...) (或具有实现的 TraversableLike )和类似方法相同的操作。 ...仅另外进行隐式转换。

更新:

我开始对此进行一些尝试,并根据 TraversableLike.map(...) 的功能编写了一个转换。然而我发现即使没有它它也能工作。看起来 Scala 开箱即用地支持它(至少在我的机器上:-)):

case class Element(e: Elem)
implicit def elemToElement(e: Elem): Element = Element(e)
implicit def elementToElem(e: Element): Elem = e.e

val a: List[Element] = List(<a/>, <b/>, <c/>)
val b: List[Elem] = List(Element(<a/>), Element(<b/>), Element(<c/>))
val c: Set[Element] = Set(<a/>, <b/>, <c/>)

这就是你想要的?

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 :-) ):

case class Element(e: Elem)
implicit def elemToElement(e: Elem): Element = Element(e)
implicit def elementToElem(e: Element): Elem = e.e

val a: List[Element] = List(<a/>, <b/>, <c/>)
val b: List[Elem] = List(Element(<a/>), Element(<b/>), Element(<c/>))
val c: Set[Element] = Set(<a/>, <b/>, <c/>)

This was what you were after?

生死何惧 2024-10-18 14:23:26

我认为这可能太过于隐含了。特别是当您可以在地图中使用转换器方法时,

val listOfElements = listOfElems map elemToElement(_)

我认为您想要的级别简洁性会变得混乱。我将创建一个转换器层并仅在其中一种表示中工作,以免事情变得混乱。

I think this might be taking implicits too far. Especially as you can just use the converter method in a map

val listOfElements = listOfElems map elemToElement(_)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文