Reputation: 662
SBT is throwing the following error:
value split is not a member of (String, String)
[error] .filter(arg => arg.split(delimiter).length >= 2)
For the following code block:
implicit def argsToMap(args: Array[String]): Map[String, String] = {
val delimiter = "="
args
.filter(arg => arg.split(delimiter).length >= 2)
.map(arg => arg.split(delimiter)(0) -> arg.split(delimiter)(1))
.toMap
}
Can anyone explain what might be going on here? Some details:
java version "1.8.0_191"
sbt version 1.2.7
scala version 2.11.8
I've tried both on the command line and also with intellij. I've also tried Java 11 and Scala 2.11.12 to no avail.
I'm not able to replicate this on another machine (different OS, SBT, IntelliJ, etc. though) and I can also write a minimal failing case:
value split is not a member of (String, String)
[error] Array("a", "b").map(x => x.split("y"))
Upvotes: 2
Views: 98
Reputation: 1754
The issue is that the filter
method is added to arrays via an implicit.
When you call args.filter(...)
, args
is converted to ArrayOps
via the Predef.refArrayOps
implicit method.
You are defining a implicit conversion from Array[String]
to Map[(String, String)]
.
This implicit has higher priority than Predef.refArrayOps
and is therefore used instead.
So args
is converted into a Map[(String, String)]
. The filter
method of that Map would expect a function of type (String, String) => Boolean
as parameter.
Upvotes: 3
Reputation: 662
I believe what happened is that the implicit method is getting invoked a bit too eagerly. That is, the Tuple2
that's seemingly coming out of nowhere is the result of the implicit function converting each String
into a key/value pair. The implicit function was recursively calling itself. I found this out after eventually getting a stack overflow with some other code that was manipulating a collection of String
s.
Upvotes: 2