5 ms·
"Actually, I wouldn’t care about for if there was an alternative. But there isn’t one." Learn the collections library. https://kotlinlang.org/docs/reference/c
by ghostc0der 10y ago
"Actually, I wouldn’t care about for if there was an alternative. But there isn’t one."
Learn the collections library. https://kotlinlang.org/docs/reference/collections.html 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.
- pyre 10y ago> 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. 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.
- simias 10y agoThat looks rather inconsistent to me. Isn't the last expression equivalent to a = (b = (c = 2)), and if not why?
- scardine 10y agoI guess it is syntax sugar for: c = 2 b = c a = b
- simias 10y agoWhat warrants this special-case sugar though? Is it somehow super convenient in python? I 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.
- uryga 10y agoPython has a bunch of special cases like this that make the code more readable (according to it's creators): `x not in xs` --> `not (x in xs)` `0 < x < 10` --> `0 < x and x < 10` `x = y = 5` --> (expanded above) `in` is used both in `for...in...:` and `x in xs` I feel like Python will gladly sacrifice being 'small and lean' if it leads to gains in readability, although they don't introduce these willy-nilly. Obviously, a lot of it is subjective, but I happen to mostly like it.
- noobermin 10y agoYou may never use them but it is used often enough that they included it as syntactic sugar.
- acoard 10y agoYou're overcomplicating it, think of it from a math perspective. Don't just look at it as the "assignment operator", look at it as an equals sign. a=b=c=2. They're all equal, all variables here equal 2.
- 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.
- rtb 10y agoI think it's a joke. https://en.wikipedia.org/wiki/Poe's_law https://en.wikipedia.org/wiki/Poe's_law