简体   繁体   中英

Scala Type Mismatch underlying type

I'm writing methods to convert Set[Tuple2[String, String]] to String and vice versa.
I'm saving the string value as v1,v2#v3,v4#v5,v6
In order to fill the Set I'm splitting the string by ',' and in order to extract the values I'm trying to split each value by '#' but i receive

type mismatch: found: x.type (with underlying type Array[String]

The code I tried using is

val x = overwriters.split("#")
for(tuple <- x) {
  tuple.split(",")
}

The returned type of split is an array of String so it is not clear to me why i cannot split each member of the returned array

overwrites.split("#").map(_.split(",")).map(x=> (x(0),x(1))).toSet

这将以更加惯用的方式实现同​​样的目标。

tuple.split(",") returns an array of two elements. You need to convert it to a tuple.

val overwriters ="v1,v2#v3,v4#v5,v6"              
val x = overwriters.split("#").toSet
for(tuple <- x) yield {
  val t = tuple.split(",")
  (t(0),t(1))
}    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM