从数组到列表的隐式转换

发布于 2024-10-17 07:16:59 字数 810 浏览 1 评论 0原文

如何编写从 Array[_]List[_] 类型的隐式转换?我尝试了以下方法,但似乎不起作用。

scala> implicit def arrayToList[A : ClassManifest](a: Array[A]): List[A] = a.toList
<console>:5: error: type mismatch;
 found   : Array[A]
 required: ?{val toList: ?}
Note that implicit conversions are not applicable because they are ambiguous:
 both method arrayToList in object $iw of type [A](a: Array[A])(implicit evidence$1: ClassManifest[A])List[A]
 and method genericArrayOps in object Predef of type [T](xs: Array[T])scala.collection.mutable.ArrayOps[T]
 are possible conversion functions from Array[A] to ?{val toList: ?}
       implicit def arrayToList[A : ClassManifest](a: Array[A]): List[A] = a.toList
                                                                           ^

How do I write an implicit conversion from Array[_] to List[_] type? I tried the following but it doesn't seem to work.

scala> implicit def arrayToList[A : ClassManifest](a: Array[A]): List[A] = a.toList
<console>:5: error: type mismatch;
 found   : Array[A]
 required: ?{val toList: ?}
Note that implicit conversions are not applicable because they are ambiguous:
 both method arrayToList in object $iw of type [A](a: Array[A])(implicit evidence$1: ClassManifest[A])List[A]
 and method genericArrayOps in object Predef of type [T](xs: Array[T])scala.collection.mutable.ArrayOps[T]
 are possible conversion functions from Array[A] to ?{val toList: ?}
       implicit def arrayToList[A : ClassManifest](a: Array[A]): List[A] = a.toList
                                                                           ^

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

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

发布评论

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

评论(2

多孤肩上扛 2024-10-24 07:16:59
implicit def arrayToList[A](a: Array[A]) = a.toList

似乎按预期工作。我的猜测是,Predef 中已经有一个 genericArrayOps,它具有从 Array[T] -> 进行隐式转换的签名。 ArrayOps[T]ArrayOps[T] 有一个方法 .toList(): List[T]。您的方法具有签名 Array[T] -> List[T],这也使 .toList[T] 方法可用。主体要求使用该签名进行隐式转换。编译器不知道使用 arrayToList 会使该方法进入无限循环,因此会出现歧义错误。然而,类型推断返回类型似乎能够解决这个问题。隐式解析似乎与类型推断不太相符。

另外值得注意的是,由于已经有一个隐式转换可以默认为您提供所需的内容,因此不需要从 ArrayList 进行隐式转换。

implicit def arrayToList[A](a: Array[A]) = a.toList

Seems to work as expected. My guess is that there's already a genericArrayOps in Predef that has the signature for implicit conversion from Array[T] -> ArrayOps[T], ArrayOps[T] has a method .toList(): List[T]. Your method has the signature Array[T] -> List[T], which also makes the method .toList[T] available. The body is asking for an implicit conversion with that signature. The compiler doesn't know that using arrayToList will make that method go into an infinite loop, hence the ambiguity error. However, type-inferencing the return type seems to be able to work around this problem. Implicits resolution don't jive very well with type-inference it seems.

Also worth noting is that since there is already an implicit conversion that will get you what you want by default, there's no need to have an implicit conversion from Array to List.

熟人话多 2024-10-24 07:16:59

从数组转换时不需要 ManifestClassManifest,因为 Array 是一种“集合”类型它在 JVM 上得到特殊处理并且不会经历类型擦除。

这意味着您可以采用明显/微不足道的方法,不需要任何技巧:

implicit def arrayToList[A](arr: Array[A]) = arr.toList

不过有一个问题...鉴于 .toList 已经是一个如此微不足道的操作,那么通过将其隐式化您可以获得什么?

There's no need for a Manifest or ClassManifest when converting from arrays, as Array is the one "collection" type that gets special treatment on the JVM and doesn't undergo type erasure.

This means that you can go with the obvious/trivial approach, no trickery required:

implicit def arrayToList[A](arr: Array[A]) = arr.toList

One question though... Given that .toList is already such a trivial operation, what do you gain by making it implicit?

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