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
linesIterator
but it is deprecated.val text : StringOps
looks realy ugly but solved it but I am unhappy with this solutionThe 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>
You can force Scala to use the implicit conversion to StringOps
, which will use the old lines
method:
(text: StringOps).lines.foldLeft("")(_ + _)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With