What is Kotlin equivalent for Java “assign and check”?

  • A+

In Java sometimes i write the code as follows:

 String obj = null;  while ((obj = getObject()) != null) {     // do smth with obj  } 

In Kotlin compile-time error is shown:

Assignments are not expressions, and only expressions are allowed in this context

What's the best equivalent in Kotlin?


I would rather give up fanciness and do it the old-school way, which is instead most intuitive.

 var obj = getObject();  while (obj != null) {     // do smth with obj     obj = getObject();  } 


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: