+ 5

Kotlin Questions.

is there any way I can do the assignment like this in println statement in kotlin? https://code.sololearn.com/cOi2wXxYvnJa/?ref=app

17th Jun 2018, 4:39 PM
Tanay
Tanay - avatar
7 Answers
+ 12
Kotlin does not allow this option. But you can do it the following way (definitely is not clean as in Java): https://code.sololearn.com/cY4JZAzhppI8/?ref=app
17th Jun 2018, 10:51 PM
LukArToDo
LukArToDo - avatar
+ 4
hinanawi yes, in this case I can assign before printing there is no harm but if have condition like this : while (fetching some data and != null ) something=fetch data again; insted of that if I write something like this : while(save fatched data to var != null) something = assign saved data then it can significantly reduce fetching time because it will fetch data only once.
17th Jun 2018, 6:17 PM
Tanay
Tanay - avatar
+ 4
I have just started learning kotlin today so didn't even reached till loop so just asking if I can write the experience like this in while loop or not.
17th Jun 2018, 6:19 PM
Tanay
Tanay - avatar
+ 3
I mean it should assign 22 to f before printing it.....can I write this kind of expression in println.
17th Jun 2018, 6:04 PM
Tanay
Tanay - avatar
+ 3
When I do something like that, I do it this way: var data = fetch() while (data != null) { process(data) data = fetch() }
17th Jun 2018, 7:44 PM
John Wells
John Wells - avatar
+ 1
which assignment are you talking about?
17th Jun 2018, 5:56 PM
hinanawi
hinanawi - avatar
+ 1
Tanay afraid it can't do that. not to knowledgeable about kotlin, but as it seems from the errors, a statement cannot be put inside a print statement. you can just put "f = 22f" in the line before, don't see anything wrong with that
17th Jun 2018, 6:06 PM
hinanawi
hinanawi - avatar