by ghostc0der · 9y ago · 0 comments
Learn the collections library. https://kotlinlang.org/docs/reference/collections.html All loops can be written in terms of them. Also because of inlining they have have the same runtime costs as hand written for loops.
"Smart cast to ‘Int’ is impossible, because ‘value’ is a mutable property that could have been changed by this time"
Smart casts only work on immutable values because of concurrency. If the variable hast to be mutable use ?.let { ... } instead of smart casts.
What's wrong with two exclamation points? If you're using it you are probably doing something wrong anyway.
When getting a type from Java that hasn't been annotated as @NonNull or @Nullable it is regarded a platform type. This means it's programmers responsibility to check if it can be null. Same as in Java or any other language that doesn't have nullable types.
Assignment is not an expression. We've written 20K of production Kotlin code without this ever being a useful convention. Assignment being an expresssion is layover from the C family of languages. If you really want C's syntax and semantics, go write C.
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
Generics in Kotlin have the same issues as Java because of type erasure. It does offer a few improvements with the use of reified type variables.
Lastly, if you want to have descriptive structures use data classes.
This guy just wants to program in C. If it's not C it's crap according to the article.
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() ?: 5LI think the author is referring to the ternary operator, not the elvis operator.
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.
for length, bytesRead in input.bytes() { //...
Edit: quick google search, the actual syntax is file.forEachBlock { bytes, size -> //...Python doesn't treat assignment as an expression. E.g. I can't do:
if a = 1:
pass
or def c():
return a = 1
or: a = lambda c: c.x = 1
But I can still do: a = b = c = 2
to set multiple variables at the same time. if not 10 < x < 20:
raise ValueError("Out of range.")
Not every language allow this. c = 2
b = c
a = bI mostly write C code and while it features these chained affectations (as a side effect of assignations being expressions) I never use them. I also seldom encounter them in the wild. Seems odd to me that a "lean and clean" language like python would bother special casing it.
a=b=c=2. They're all equal, all variables here equal 2.
In maths a = b = 2 is the same as a = (b = 2), (a = b) = 2, (a = b = 2) or a = 2 = b. Clearly it doesn't apply here.