Nikolay
Nikolay

Reputation: 53

What is going on in the match functionality?

I have a method:

  def replaceSpecialSymbols(str: String): String = str.collect {
    case '/'     => '-'
    case _ => _
  }.toString

Whe I try to build this code, I receive the error message: "error: unbound placeholder parameter case _ => _"

I know that I can use replaceAll. But I want to know what is going on in this case in Scala compiler.

Thank you.

Upvotes: 4

Views: 309

Answers (1)

Erik Kaplun
Erik Kaplun

Reputation: 38217

Use case x => x — problem solved. Also, you can just use map instead of collect because it's an exhaustive match.

Or if you only need the first case, just remove that case _ => _ altogether and keep using collect.

Upvotes: 5

Related Questions