I am working with Scala's implicit class mechanism and can't get java.lang.String recognized as Iterable[A].
implicit class PStream[E](stream: Iterable[E]) {
def splitOn(test: Iterable[E] => Boolean): Option[(Iterable[E], Iterable[E])] = {
???
}
}
With just the above definition, IntelliJ and SBT state that ...
Error:(31, 8) value splitOn is not a member of String possible cause: maybe a semicolon is missing before `value splitOn'? .splitOn(s => Character.isWhitespace(s.head))
... when I try this ...
line: String =>
val Some((identifier: String, patternn: String)) =
line
// take the identifier
.splitOn(s => Character.isWhitespace(s.head))
That's because
Iterable[E]is a Scala trait, and as such a relatively "recent" invention, whereasjava.lang.Stringis a fundamental Java datatype that has been there since version 1.0. from 1995. Obviously,java.lang.Stringdoes not implementIterable[E].Moreover, even if there is an implicit conversion from
StringtoIterable[E], Scala will never attempt more than one implicit conversion on a single expression.If you want to pimp
String, you have to passStringas a single parameter to your implicit class. The compiler will refuse to build crazy towers of multiple implicit conversions, because it would make the compilation time inacceptable otherwise.What you could try instead would be something like this:
and then provide a separate
This would give you pretty much the same functionality, but it would not require an uncontrolled explosion of implicit conversions.