Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()) {}
like image 604
Nulano Avatar asked Mar 26 '14 12:03

Nulano


1 Answers

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

while ({
  val m = pattern.matcher(s)
  m.matches
}) { ... }
like image 176
serejja Avatar answered Nov 07 '22 05:11

serejja