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.


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


「福强私学」来一个?

「福强私学」, 一部沉淀了个人成长、技术与架构、组织与管理以及商业上的方法与心法的百科全书。


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

订阅「福报」