samba
samba

Reputation: 3101

Scala - pattern matching - Constructor cannot be instantiated to expected type

I'm getting a String of comma-separated metric values and then using the String for an SQL query.

However when I'm trying to pattern-match, there's an Error here: Some(newMetrics) - option Constructor cannot be instantiated to expected type, found: Some[A], required: String

What is the right way to use pattern-matching in this case?

val metrics: String = props.getProperty(NEW_METRICS).filter(StringUtils.isNotBlank).getOrElse("")

val metricsQuery = metrics match {
  case Some(newMetrics) => s"""SELECT $newMetrics FROM ${metricsTable}"""
  case _ => OLD_TABLE
}

Upvotes: 0

Views: 1215

Answers (1)

senjin.hajrulahovic
senjin.hajrulahovic

Reputation: 3191

You are trying to match a String as if it were an Option[String]

By leaving out the .getOrElse("") metrics will be of type Option[String] and you can match it as you did in the lower part of your example.

Upvotes: 4

Related Questions