Nulano
Nulano

Reputation: 1333

Assign to a variable in the condition of a while loop - scala

I Java I can write this:

Matcher m;
while ((m = pattern.matcher(string)).matches()) {...}

How would I do this in Scala? This doesn't work :

var m: Matcher = null
while ((m = pattern.matcher(s)).matches()) {}

Upvotes: 5

Views: 3829

Answers (1)

serejja
serejja

Reputation: 23851

Assignments return Unit in Scala, but it's ok to use code blocks like this:

while ({
  val m = pattern.matcher(s)
  m.matches
}) { ... }

Upvotes: 9

Related Questions