Equivalent of "case" filters in for-comprehension for assignment

Hello,

I like the feature of Scala 3, where you can further filter according to a match:

for {
  ...
  case JsString(string) <- jsObjects
}

But there is not an equivalent for assignment, which would require writing this:

for {
  ...
  if jsObject.isInstanceOf[JsString]
  string = jsObject.asInstanceOf[JsString].stringValue
}

I have since started came up with a nicer “hack”, where I wrap the value in Some, for example:

for {
  ...
  case JsString(string) <- Some(jsObject)
}

Since Scala promotes orthogonality, why does Scala not allow case for asignment as well. This example should compile:

for {
  ...
  case JsString(string) = jsObject
}

What do you think?

2 Likes

I also think this would be a good generalization. I had proposed the same in the currently open SIP for for expressions.

2 Likes