Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala 12.x and Java 11 `String.lines`, how to force the implicit conversion in a chained call?

Problem description Scala StringOps provides a lines method that returns an Iterator[String]. Java 11 added lines() with return type java.Stream[String].

In a chained method call like

val text: String
text.lines.foldLeft("")(_ + _)

the code will no longer compile and throw an exeption that foldLeft is not defined on java.Stream[String]. As far as I understand the implicit resolution is no longer applied as the lines method now is already found in java.String.

How can I express that I want the implicit to be applied (the one without parens) isntead of the java.String.lines()

Additional info

  • I found linesIterator but it is deprecated.
  • Downgrading is an option but is there a way around it.
  • val text : StringOps looks realy ugly but solved it but I am unhappy with this solution
like image 227
Andreas Neumann Avatar asked Oct 15 '18 11:10

Andreas Neumann


2 Answers

The conflict between StringOps#lines and jdk11 java.lang.String#lines is a bug in scala, see issue 11125.

The fix for this bug is to un-deprecate linesIterator, which was done in 2.12.7.

Welcome to Scala 2.12.7 (OpenJDK 64-Bit Server VM, Java 11).

scala> "a".lines
res0: java.util.stream.Stream[String] = java.util.stream.ReferencePipeline$Head@2df259d0

scala> "a".linesIterator
res1: Iterator[String] = <iterator>
like image 97
obourgain Avatar answered Nov 13 '22 01:11

obourgain


You can force Scala to use the implicit conversion to StringOps, which will use the old lines method:

(text: StringOps).lines.foldLeft("")(_ + _)
like image 32
Clashsoft Avatar answered Nov 13 '22 03:11

Clashsoft