8

I'm trying to port some Java code into Scala:

while ((j=f('blah'))>=0) ...

ERROR:"value >= is not a member of Unit"

Is this not possible?

Ben James
  • 121,135
  • 26
  • 193
  • 155
sucasa
  • 373
  • 1
  • 8
  • 19

1 Answers1

23

Assignments return () (unit) in Scala. But that's okay because you can put a code block anywhere. You need this instead:

while ({ j=f("blah"); j } >= 0) ...
Rex Kerr
  • 166,841
  • 26
  • 322
  • 407
  • 7
    The reasoning for that can be found here: [What is the motivation for Scala assignment evaluating to Unit rather than the value assigned?](http://stackoverflow.com/questions/1998724/what-is-the-motivation-for-scala-assignment-evaluating-to-unit-rather-than-the-v) – EECOLOR Mar 03 '13 at 23:23