Live data from Hacker News

Learn Kotlin in Y Minutes

learnxinyminutes.com

211–220 of 239 posts

Re: Learn Kotlin in Y Minutes

#211

Earlier quoted context omitted.

Thankfully that would be a simple compile-time error. The message might even be readable.

If you type var and mean val - would there be any error? (I'm sure that this is just something you need to get used to, I'm merely trying to point out that this could be a silent typo/mistake)

As others said, not a problem if you are using an IDE (and a specific one.)

Considering who authored the language, I am not surprised at all.

Re: Learn Kotlin in Y Minutes

#212
post #132

I like this val z = (1..9).map {it * 3} .filter {it I'd like it more if it said it performed the functions in parallel. If it does this Automagically I'm sold. If it doesn't, is there an easy to use form that does do batch operations like map in parallel?

Parallel version:

    (1..99).parallelStream()
           .map { it * 3 }
           .filter { it  { 
               if (it % 2 == 0) "even" else "odd" 
           }))
The result is of type ConcurrentMap>

A couple of notes about this code:

1. It uses the Java 8 streams API. I'm not sure it'll work on Android. The code you gave uses the Kotlin standard library extension functions, which is a different implementation of map/filter/etc using inline functions. When possible, use the Kotlin versions, which don't create new garbage or methods. But Kotlin's library is less powerful than the Java streams API, which can do automatic parallelism.

2. For this toy example the parallel code will be slower. There is a cost involved in automatically parallelising an operation that won't even begin to pay off until you're working with very large inputs.

3. Due to a bug in how Kotlin's type inference interacts with the Java 8 Streams.collect call, I have to specify the type of the lambda explicitly, which is a bit ugly. Especially as the IDE and compiler don't quite agree, so the IDE renders Function in grey, but if I remove it as suggested, I get a compile error. Such bugs are very rare (in fact Streams.collect is the only place I've seen it happen lately), but do highlight the fact that Kotlin has only been at version one for a year and is still maturing the level of stability you would expect from Java.

Re: Learn Kotlin in Y Minutes

#213
post #177
post #164

Does Kotlin have its own class/libraries or it still uses Java's classes?

Kotlin has its own standard library

It's both.

Kotlin's standard library is tiny. It consists mostly of extension functions and aliases over the Java standard library. For instance kotlin.String is just an alias to java.lang.String, it's exactly the same object. Kotlin's lists are just more type safe views over java.util lists. Much of Kotlin's standard library is in fact inlined and then optimised, so using it doesn't even create function calls in your bytecode.

So whilst Kotlin does technically have a standard library, to do anything practical you'll be using Java's. And that's one reason why we like it. No duplicate standard library to learn.

Re: Learn Kotlin in Y Minutes

#214

Earlier quoted context omitted.

It's not redundant; for example let (mut x, y) = (1, 2); x is mutable, y isn't. That is, let is the way that you introduce a new binding, always.

Not OP, but why do you need the 'let' at all? Why not x = 7 mut y = 3 ?

Because it's a bit too error-prone:

   mut listOfLeftHandedOralHygienistsInKazakhstan = getThem()
   // several lines later
   listOfLeftHandedOralHygienistsInKazahkstan = getThemAgain()
You thought you were reassigning the mutable variable, but you actually created a different immutable variable.

This can be prevented by having different operators for assignment and re-assignment. Some languages do that: in OCaml / F#, re-assignments use '<-' while declarations use 'let (mutable) x =' (they can't drop the 'let' because they use '=' as the Boolean equality operator).

Re: Learn Kotlin in Y Minutes

#217

Does it have an equivalent to Swifts if let clause? if let person = selection?.organization?.owner { setTitle(person.name) setImage(person.image) }

Is this setting a value to the name `person`? What is the value?

selection?.organization?.owner is an optional value, meaning it might be nil/null. That "if let" basically says if it's not nil to assign its value to "person" and execute the block. Then inside the block you can treat "person" as a non-optional, known value.

Re: Learn Kotlin in Y Minutes

#218
post #105

It looks like there's been a very conscious effort to strike a midpoint between Java's familiar clunkiness and Scala's hipster experimental side. I think that's a good thing, you end up with a language that feels pragmatic and powerfully practical.

I've been told that a big selling point for Kotlin is that it was developed from actual usage and production rather than some of the more academic languages solving abstract (although still interesting) problems that day-to-day developers may not care much about.

The Henry Ford quote about faster horses comes to mind.

Re: Learn Kotlin in Y Minutes

#219
post #132

I like this val z = (1..9).map {it * 3} .filter {it I'd like it more if it said it performed the functions in parallel. If it does this Automagically I'm sold. If it doesn't, is there an easy to use form that does do batch operations like map in parallel?

I guess they're just showing off mapKeys, but couldn't that line be eliminated with: .groupBy {if (it % 2 == 0) "even" else "odd"}

[deleted]

Re: Learn Kotlin in Y Minutes

#220
post #218
post #105

Earlier quoted context omitted.

I've been told that a big selling point for Kotlin is that it was developed from actual usage and production rather than some of the more academic languages solving abstract (although still interesting) problems that day-to-day developers may not care much about.

The Henry Ford quote about faster horses comes to mind.

You're trying to tell us that Scala is something better and we are just too stupid to understand it's power/beauty/importance?
Post reply on HN