0

I have a list that contains a mixture of strings and maps, listOfIds. The code below attempts to this list. Maps into matched and strings to unmatched.

val (matched, unmatched) =
  listOfIds.foldLeft(List.empty[Map[String, String]], List.empty[String]) {
     case ((matched, unmatched), p) => p match {
       case m:Map[String, String] => (m :: matched, unmatched)
       case s:String => (matched, s :: unmatched)
     }
   }

The issue is I'm running into type erasure. I found that I was able to use place holders for the types to get around this m:Map[_, _],but this causes issues later on when attempting to extract the map values.

I'm unfamiliar with type tags but quick research suggests that this could be a solution... Would appreciate any points how I could get around type erasure in this instance.

Thanks!

kidshenlong
  • 552
  • 6
  • 16
  • 1
    You can find a solution to your `type erasure` problem here : http://stackoverflow.com/questions/1094173/how-do-i-get-around-type-erasure-on-scala-or-why-cant-i-get-the-type-paramete . `foldLeft` might not be the optimal method here. Did you have a look at `partition` in the `Seq`-Trait , this looks like a good fit? – Andreas Neumann Dec 29 '15 at 16:21
  • The issue with partition is that the returns lists have the same signature. I suppose this isn't the worst issue, as I'd just need to flatten it but still. Thanks for the link, I'll see what I can throw together now. – kidshenlong Dec 29 '15 at 16:27
  • What is the type of `listOfIds`? – Kevin Meredith Dec 29 '15 at 16:32
  • Atm it's `List[Object]` – kidshenlong Dec 29 '15 at 16:34
  • What error are you getting? Seems to work for me ... – Dima Dec 29 '15 at 16:45

0 Answers0