BiasedScala: collection converters should be gone

王福强

2021-10-27


Scala offers its own collection framework besides java’s collections. But most of the time, I use more java’s collection than scala’s. So that means, I have to import converters to convert to or convert from java collections or scala collections. This is annoying in my opinion.

Furthermore, in Scala2.12.x and Scala2.13.x, the collection converters are different, I have write down a note for this or put two code snippets in my Dash.app, Consistency, where are you?!

To use collections of Java with FP support, We have to do it this way in Scala 2.12.x:

import scala.collection.JavaConverters._

val s: java.util.Set[String] = Set("one", "two").asJava
val s2 = FileUtils.listFiles(...).asScala

In Scala 2.13.x this way:

import scala.jdk.CollectionConverters._

val s: java.util.Set[String] = Set("one", "two").asJava
val s2 = FileUtils.listFiles(...).asScala

It’s not elegant and necessary, I think, especially when Scala 3 has introduced extention mechanism, If I want to use Java collections with additional FP support which scala collections offer, I should use these FP support directly without import anything:

FileUtils.listFiles(...).foreach(file=> ...)

No more import converters, No more asScala/asJava things, that’s what I think a good design.

Sadly, I can NOT see this currently in Scala 3.


>>>>>> 更多阅读 <<<<<<


欢迎加入「福强私学」

跨越2190个日夜,始终坚持“实践 + 原创”打造的715125字专属知识库,囊括了(但不限于)从职场、技术、管理与商业等多个板块的内容。

  • 一个ChatGPT触达不到的地方
  • 一个带你超越AI/人工智能的地方
  • 一个与你一起成长的地方

https://afoo.me/kb.html


开天窗,拉认知,订阅「福报」,即刻拥有自己的全模态人工智能。

订阅「福报」