4 ms·
> Assignment is not an expression. We've written 20K of production Kotlin code without this ever being a useful convention. Out of curiosity, what is the idiom
by mmebane 10y ago
> Assignment is not an expression. We've written 20K of production Kotlin code without this ever being a useful convention.
Out of curiosity, what is the idiomatic way of writing an input loop? The C/Java way would be something like
int bytesRead = 0;
while ((bytesRead = in.read(buf)) != -1) { // ...
> The ?: operator is great when chaining a bunch of operations all of which could return a null which we want to convert to a default. Here is an example: claimData?.claimTimeMinutes?.toLong() ?: 5L
I think the author is referring to the ternary operator, not the elvis operator.
- Tarean 10y agoI have no experience in kotlin but many other languages that use iterators would go with something like for length, bytesRead in input.bytes() { //... Edit: quick google search, the actual syntax is file.forEachBlock { bytes, size -> //...
- nostrademons 10y agoMost likely: in.buffered.readText() The Kotlin stdlib has a rich set of extension methods for reading in whole files of various common formats at once. readText() reads it all into memory at once. readLines() reads it into a list of Lines. forEachLine{} runs a closure on each line. useLines reads it into a sequence (a lazily-evaluated collection) and then passes that into a closure. buffered converts it to a BufferedReader, basically wrapping it in your buffering loop. inputStream converts it to an InputStream, reader to a Reader, and most common Java protocol libraries take one of those two, so if you need something more complicated you just pass that in. I've yet to write an input loop in Kotlin.