19 ms·
Learn Kotlin in Y Minutes
- subsidd 9y agoKotlin's taken over HN.
- softawre 9y agoDeservedly so. It is a great language, and it's backed by the ultra-power JVM. You can use a hipster language with great features and still use JodaTime and maven and all the enterprise stuff that just works.
- subsidd 9y agoAgreed.
- betatest 9y agoWhat exactly make it a great language? What are the advantages compared to Java? I search for it but couldn't find any simple answer to that question. The fact that things are shorter to declare doesn't make it better. It makes it less readable.
- torinmr 9y agoI haven't used Kotlin, but glancing over their comparison page the one that jumped out to me was that it (almost) eliminates the possibility of null pointer exceptions: https://kotlinlang.org/docs/reference/null-safety.html https://kotlinlang.org/docs/reference/null-safety.html
- thatswrong0 9y agoThis is my litmus test to determine whether a modern language is not terrible. Why wouldn't you want to eliminate an entire class of errors? Unfortunately, my day to day language at work is Go.
- tathougies 9y agoThis is like an entry-level expectation for a modern language. Hardly something to write home about in this day and age.
- fixermark 9y agoIt is when you bring it first-party to a platform the size of Java. The userbase of Objective-C was much smaller before OSX brought Cocoa to the Mac ecosystem.
- itp 9y agoFor advantages (and some disadvantages!) compared to Java, I'd start with https://kotlinlang.org/docs/reference/comparison-to-java.html https://kotlinlang.org/docs/reference/comparison-to-java.htm....
- ionforce 9y agoA wide array of quality of life changes to Java. > The fact that things are shorter to declare doesn't make it better. This is debatable. In fact, shorter code could (but not always, of course) mean less room for error and more room to hold higher abstractions. Boilerplate is a less efficient use of cognitive energy.
- jakub_g 9y agoAgreed, with an exception of magic non-standard out-of-context characters (#$=%>^<) with non-intuitive language-specific meanings; see bash, perl, angular... I prefer short textual keywords 99% of the time
- kbenson 9y ago> The fact that things are shorter to declare doesn't make it better. It makes it less readable. That's not a hard and fast rule. There are obvious inflection points. A 200 character long identifier is less readable than a 20 character identifier in almost all cases. A 1 character identifier is less readable than a 8 character identifier in almost all cases. Somehwhere between those extremes is the sweet spot, and it's likely somwhat different depending on person. As for language keywords, as long as they are unique, unambiguous, and not likely to conflict with written code often, shorter is likely better since it's purely a matter of learning them when learning the language. Given those constraints, it's easy to see it's possible to be too short though (any single character keyword that is not context sensitive is likely a horrible choice).
- sevensor 9y agoSteve Yegge drops a blog post, and we all freak out :) He needs to get back on a regular scheudle so we can react like reasonable people to the next one.
- dtech 9y agoProbably a spike in interest/activity/articles because of Google's announcement that it now is officially supported on Android.
- subsidd 9y agoAlso because some of us absolutely needed a break from java.
- miguelrochefort 9y agoStill looks like Java.
- Filligree 9y agoNot past the first day. Kotlin code can be beautiful, and I've yet to see beautiful Java code.
- dcow 9y agoNot even a bad thing. It's a pragmatic JVM language. It's 100% interoperable with Java without baggage like Groovy has. It's probably going to look like Java. Scala can look like Java if you write it very imperatively. What's nice about Kotlin is that the language supports idioms that allow you do diverge from "looking like Java" as you write it more.
- vorg 9y agoHope we never see "looks exactly like Java syntax but behaves differently when you run it" like we see in Apache Groovy, e.g. their == operator acts differently to Java's.
- meddlepal 9y agoPretty much every language's `==` operator acts differently and more sanely when compared to Java's. Groovy had a very different initial design philosophy compared to Kotlin. The Groovy design philosophy was give Java developers a good highly-dynamic scripting language that still felt like Java but also made it more suitable for writing quick and dirty plugins, scripts and other extension bits to Java programs. Kotlin is more in the "Java is great already, but has a lot of legacy issues, let's take all the stuff learned in the last two decades and make a Java++"
- grabcocque 9y agoIt 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.
- mejari 9y agoI'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.
- piaste 9y agoThe Henry Ford quote about faster horses comes to mind.
- romanovcode 9y agoYou're trying to tell us that Scala is something better and we are just too stupid to understand it's power/beauty/importance?
- hocuspocus 9y agoScala is undeniably more powerful overall (beauty and importance are subjective). It's also used by companies like Verizon, SAP, IBM, Walmart ... not exactly hipster research labs.
- jfim 9y agoWhich is funny, because Scala is a pragmatic mix that's halfway between more "pure" functional languages and Java. Guess the sweet spot is a quarter of the way there, not halfway. :)
- alexfi 9y agoKotlin - The Force Awakens
- skdotdan 9y agoKotlin is the new Swift/Golang #hype
- IshKebab 9y agoNot really, it's more like "Oh my god we don't have to use Java any more". I don't think it is particularly loved on it's own, it's just much better than Java. Swift is similar in that iOS developers don't have to use Objective C any more, but actually looks like a nice language in its own right. I think although Kotlin is going to be an improvement for Android development, it's not going to change much unless Google drastically simplify the actual Android API. You're still going to have to deal with this shit: https://github.com/xxv/android-lifecycle https://github.com/xxv/android-lifecycle
- lucidguppy 9y agoWasn't scala supposed to do that?
- ungzd 9y agoMore like coffeescript for Java
- h1d 9y agoWhich died. More like TypeScript.
- shubb 9y agoIn terms of why we'd want to, there is this note here: https://kotlinlang.org/docs/reference/comparison-to-java.html https://kotlinlang.org/docs/reference/comparison-to-java.htm... Other factors to consider are tools support, how often interoprability with java libraries causes problems. Would be nice to hear from someone who has used it a bit about what the state of these is?
- betatest 9y agoCome on. This is not about actual production stuff. It's new! It's cool! That's pretty much it.
- dcow 9y agoNo so. Many people use it in production. The language has been around for 7 years and a stable 1.0+ release for over a year. It's very much more mature than swift and rust and certainly sibling to go in that regard.
- jorgemf 9y agoI tried to explain exactly this before and a lot of people didn't get it. 1.0 doesn't mean anything, JetBrains spent so much time to make something stable and good. That is why kotlin didn't suffer of the big changes swift suffered since its 1.0. (because kotlin suffered those changes before 1.0)
- EugeneOZ 9y agonot trying to say against Kotlin, but Rust has 2 years after 1.0 :)
- on_and_off 9y agoI can talk about this in the context of Android. First I should point out that many large Android apps are using Kotlin. There is a lot of momentum behind Kotlin on Android, that's why Google adopted it. Tools support is really good. Jetbrains knows how to create tools and it shows. Using kotlin also shows you how great the android plugin for java is. Nothing major, but little features like shortcuts to create a string/drawable resource are sometimes missing in kotlin. Jetbrains is missing that gap though, and we should quickly reach parity now that the language is officially supported. Interop with Java is equally great ! Both java & kotlin output bytecode, so they can dialog easily. Some interop caveats : - sometimes calling java from kotlin is not idiomatic. However nowadays many libs are already offering kotlin bindings and again, this should improve. - similarly, calling kotlin from java can be a bit awkward syntactically speaking. - kotlin guarantees nullability behavior, but this can be broken at the interface with java in a couple of cases. If the java code lack @nonnull @nullable annotations, it is up to you to handle their nullability. Similarly, kotlin classes instantiated from java code can mess with nullability. If you data class contains a nonnull property and you instantiate this class with Moshi (a json parser), if the corresponding json field is missing you end up with a null field on a nonnull property. So it is pretty rare, but you can still end up with some NPE.
- notamy 9y agoHonestly I could never really get into Kotlin. I like the features of the language - and would love to have many of them in plain ol' Java - but the syntax just feels wrong to me and I've not been able to get over it.
- shallot_router 9y agoI find it a huge improvement over Java, but I wish it would have more syntax sugar for lists/arrays/maps like Swift and dynamic languages do. The mix of "hip" programming language constructs with unchanged Java verbosity feels weird to me. But if I were making an Android app, I'd definitely try to do it with Kotlin first.
- WaxProlix 9y agoWhat kind of sugar are you looking for? I'd like to have first-class comprehensions in any language I use, but having decent map/filter/reduce|fold implementations is good enough for the most part. (1..100).map({ it + 10 }) Doesn't seem so bad.
- dcow 9y agoYou can even get rid of the final parentheses. (1..100).map { it + 10 }
- WaxProlix 9y agoSomehow that seems even less readable to me.
- lenkite 9y agoThis is simply "parentheses in a call can be omitted entirely if the lambda is the only argument to that call." This one feature allows for type safe DSL's which eases GUI construction on Android and other domains. https://github.com/Kotlin/anko/wiki/Anko-Layouts https://github.com/Kotlin/anko/wiki/Anko-Layouts Its also used for chaining stream code which takes in functions. You get used to this feature very quickly.
- westoncb 9y agoIs anybody here using Kotlin for back-end work? It seems like I generally hear Kotlin come up in connection with Android, and less for writing server code, though that's where I'd potentially like to use it.
- vbezhenar 9y agoI've used Kotlin for standalone Java programs, it's great. According to Jetbrains, it's roughly 50%:50% between Android guys and Java guys. I guess, after yesterday announce it'll shift to Android. For example Spring announced that Kotlin will be first class citizen with next major release. So things are definitely bright on the server side.
- asadmshah 9y agoI've used it to write a real time HN clone using GRPC. It was a wonderful experience. The Java interop is no joke, it works flawlessly. When you have the option to use Java 8 without caveats (unlike in Android), the benefits of Kotlin aren't really _that_ visible. I'm never going back to Java on Android though.
- Xantier 9y agoI've been doing few projects with it and find it very lovely. Interop with Java works flawlessly in my experiences and RxKotlin extending RxJava makes that side work nicely as well. For one project[0] I used Ratpack as the web tier and that with Guice locked in place very easily. DB side of things I have used Kotlinquery which did its job nicely for my small use case. In another I used Spring Boot where Kotlin felt like a native in that environment. I'm guessing if you are building something with ORMs or similar where Java Bean convention is needed, you might lack the beauty of immutability and data classes (there are workarounds though) but other than that most pieces drop in place without a hitch. 0: https://github.com/Xantier/trycatch https://github.com/Xantier/trycatch
- mike_hearn 9y agoYes. I lead a team that's writing a new piece of what can be thought of as enterprise middleware in Kotlin: https://github.com/corda/corda https://github.com/corda/corda It's a distributed ledger/blockchain system being written for large, conservative banks and is around ~90%+ Kotlin code. There's not much to say about it, it works fine, I wrote about it here: https://www.reddit.com/r/programming/comments/6bqo7n/kotlin_on_android_now_official/dhppfvo/ https://www.reddit.com/r/programming/comments/6bqo7n/kotlin_...
- maaaats 9y agoKotlin is more than just simpler java-syntax, though. If you want to really take advantage of it, there's new idioms, you can use it as a typesafe dsl etc.
- Grazester 9y agoEasier syntax??!!
- seanalltogether 9y agoDoes it have an equivalent to Swifts if let clause? if let person = selection?.organization?.owner { setTitle(person.name) setImage(person.image) }
- asadmshah 9y agoYes. val person = selection?.organization?.owner?.let { setTitle(it.name) setImage(it.image) it }
- metroidfan832 9y agoYou can use `also` instead of `let` to remove the last `it` line.
- amatheus 9y agoYou may use `also`. It is like let but it returns the receiver. val person = selection?.organization?.owner?.also { setTitle(it.name) setImage(it.image) }
- seanalltogether 9y agook so I'm guessing you can't operate on more then one scope at once? if let person1 = dashboard?.owner, person2 = selection?.organization?.owner { person1.cloneProperties(person2) }
- kodablah 9y agoYou'd use nested let clauses and name the param instead of using the implicit "it"
- meddlepal 9y agoOr just write a function to accomplish the same behavior: http://stackoverflow.com/a/35522422/364135 http://stackoverflow.com/a/35522422/364135 Would be nice if there was a multiLet in the stdlib. Probably something that happens eventually anyways.
- _pmf_ 9y agoI'm a bit disappointed that there's no metaprogramming support in Kotlin. Even Groovy had some AST-level transformators. Maybe we'll get some high-level API for writing APT-level tools in Kotlin itself ...
- marcofiset 9y agoIf you're looking for metaprogramming on the JVM, and like the semi-functional approach of Kotlin, you should definitely take a look at Clojure.
- killin_dan 9y agoI hate clojure, but that complaint is EXACTLY what I was thinking must drive clojure users to it in the first place. Clojure just seems dumb because the jvm is such a hassle and bloated, sbcl is incredible, racket is good, guile is incredible, etc plenty of other lisp environments, why waste time on java? I hope kotlin native is as good as scala native. I don't care about kotlin vs Java, but kotlin vs scala native is HUGE to me.
- ungzd 9y agoMaybe inline functions may be used for some basic metaprogramming, but I'm not sure https://kotlinlang.org/docs/reference/inline-functions.html https://kotlinlang.org/docs/reference/inline-functions.html
- coldpizza 9y ago> Declaring values is done using either "var" or "val". "val" declarations cannot be reassigned, whereas "vars" can. I wonder why they decided on these very mistakable names. Why not const/constant/cons/whatever else just as long it's distinguishable from each other?
- bschwindHN 9y agoMy guess is it came from Scala? In practice it's not very tough to get it right.
- throwaway7645 9y agoVal might have originally been OCaml or Haskell.
- mahmud 9y agoStandard ML, rather. It might have appeared in earlier MLs as an extension.
- throwaway7645 9y agoAnd I was soooo close :)
- bschwindHN 9y agoI mean the combination of val (immutable) and var (mutable) as keywords to declare bindings. OCaml uses let and so does Haskell, right?
- vnglst 9y agoI'm guessing here: With const there's always the disccusion about consts not being constant, e.g. when referencing an array or object (reference itself is constant). With "val" you don't have that confusion?
- 9y ago
- k__ 9y agoI have to damit, I wish Ceylon would have won, hehe.
- leshow 9y agoDoes Kotlin have algebraic types? Possibly via sealed traits or the like, similar to Scala?
- dcow 9y agoKotlin has sealed classes. It also has built in generic Pair and Triple containers. But no it doesn't really have algebraic types as you see in Haskell. For example, you can't build a linked list by `cons`ing Ints and Strings and then take the dot product with another Int-String list. It still relies on generics in that regard although it does make improvements with reified types (generics that are retained at runtime for use in type checking `is`--kotlin `instanceof`) and variance indicators.
- jorgemf 9y agoI like kotlin a lot, but suddenly there are a lot of articles in the front page about it. Kotlin isn't more cool because Google announced yesterday it is going to use it in Android. Kotlin is the same good language it was 2 days ago! (rant over) If you want to learn kotlin go to the official docs and tutorial, they are as good as the language: http://kotlinlang.org/docs/reference/ http://kotlinlang.org/docs/reference/ And you don't need to install anything as you can try it in the browser: https://try.kotlinlang.org https://try.kotlinlang.org
- hn_throwaway_99 9y agoOfficial support still matters. There is some inherent risk whenever you go in a direction not explicitly supported by the owner of your platform, and now that risk is gone.
- jjoonathan 9y agoNot only does official support matter, it matters much more than whether or not the language is good. I keep learning that lesson again and again in the hard, painful way.
- jorgemf 9y agoOut of curiosity, What was your bad experiences?
- TJ-14 9y agoAs a new developer I'd like to hear more about why this is?
- smitherfield 9y agoIf a language or tool is officially supported, then that means the project maintainers (Google, in this case) promise to support its use with official documentation, priority bug fixes (for example, if an Android update breaks your app which is written in Kotlin and uses only public, non-deprecated APIs, Google will consider it just as high-priority a bug as if it were written in Java), and first-class access to new APIs as soon as they're created.
- eranation 9y agoBeen watching Kotlin for a long time. Usually it pops out when I talk about how great Scala is, and I get Kotlin as "what Scala should have been". I have been avoiding using it at work instead of Java/Scala just because Scala had more "buy in" and it was easier to convince people to try it. Now with the new Google/Android (and HN) love, this is the perfect time to give it another try.
- jbeja 9y agoI would like to love Kotlin, but I wish it have proper macros.
- maxpert 9y agoI knew this will show up ASAP I heard the Kotlin support news :P
- gcb0 9y agothe whole kotlin debate is just a misguided stab at solving the pain that is programming for android. everyone everywhere hates android dev work. so they think that it must be java. no, java sucks, but kotlin sucks just the same. the problem is the android ecosystem as a whole. the ever changing apis. etc. wasting time on java vs kotlin is absurd with so many other real problems.
- bitmapbrother 9y agoGod, what a useless post. If you don't like Android development don't waste your time with it.
- h1d 9y agoCriticism can be useful if constructive.
- dragonwriter 9y agoBut that criticism wasn't constructive, so...
- gcb0 9y agoit was constructive in the sense that the debate waste time from the real issue. But feel free to keep adding noise and fighting for syntax sugar.
- deleted 9y ago[deleted]
- SadWebDeveloper 9y agoProbably everyone will try to hide your comment but that's the truth, there hasn't been a worse ecosystem than Android, learning the Android SDK it's so painful that even if they choose a decent language like Python to support it won't change the fact that the APIs would still be called in the same way, just look at the examples, same just sugar-coated flavor: https://github.com/JetBrains/kotlin-examples/blob/master/gradle/android-mixed-java-kotlin-project/app/src/main/kotlin/org/example/kotlin/mixed/KotlinActivity.kt https://github.com/JetBrains/kotlin-examples/blob/master/gra... vs https://github.com/JetBrains/kotlin-examples/blob/master/gradle/android-mixed-java-kotlin-project/app/src/main/java/org/example/kotlin/mixed/JavaActivity.java https://github.com/JetBrains/kotlin-examples/blob/master/gra...
- Lerc 9y agoI like this val z = (1..9).map {it * 3} .filter {it < 20} .groupBy {it % 2 == 0} .mapKeys {if (it.key) "even" else "odd"} 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?
- guelo 9y agoNo, not parallel. Though it should be possible to add extension functions to Collection that use Java 8's Stream apis to provide that functionality.
- runT1ME 9y agoScala has parallel collections that allow for this, looks exactly the same syntax too.
- fineline 9y agoI guess they're just showing off mapKeys, but couldn't that line be eliminated with: .groupBy {if (it % 2 == 0) "even" else "odd"}
- deleted 9y ago[deleted]
- mike_hearn 9y agoParallel version: (1..99).parallelStream() .map { it * 3 } .filter { it < 20 } .collect(Collectors.groupingByConcurrent(Function<Int, String> { if (it % 2 == 0) "even" else "odd" })) The result is of type ConcurrentMap<String, MutableList<Int>> 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<Int, String> 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.
- deleted 9y ago[deleted]
- fixermark 9y agoTo a first approximation, it seems like Java : Kotlin :: JavaScript : TypeScript This seems like a good idea.
- alexellisuk 9y agoDoes anyone know the official docker image for Kotlin or are we too early for that?
- neoncontrails 9y agoCould someone paraphrase the argument for learning Kotlin? I understand it's essentially Jetbrain's fork of Java, motivated to some degree by the desire to provide useful realtime information to programmers working in the Jetbrains ecosystem. Presumably this implies that certain features present in Java that are ambiguous at compile time (e.g. generic types) might be winnowed away in the pursuit of a language more conducive to static analysis?
- justsid 9y agoIt’s not a fork of Java, it’s a language that uses the JVM like Scala. It’s not at all like Java, it’s a modern language more akin to Swift with lots of syntactic sugar.
- ausjke 9y agoDoes Kotlin have its own class/libraries or it still uses Java's classes?
- sedro 9y agoKotlin has its own standard library
- mike_hearn 9y agoIt'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.
- stewbrew 9y agoI think the language is quite nice but the tooling is lacking (if you don't use intellj idea). Even the official eclipse plugin has many rough edges. So, when learning kotlin, you effectively also have to learn an ide if you're not already an idea user.
- lucidguppy 9y agoSo with python I have virtual env. With elixir I have mix. For javascript I have npm. What do I have for kotlin?
- e12e 9y agoMaven?
- partycoder 9y agoThis webcomic explains it better than I ever could: http://abstrusegoose.com/249 http://abstrusegoose.com/249
- brianwillis 9y ago> If a lambda has only one parameter then its declaration can be omitted (along with the ->). The name of the single parameter will be "it". > val notPositive = not {it > 0} This idiom took me by surprise. It's really quite lovely, and now I'm disappointed C# doesn't include it.
- pvdebbe 9y agoThe schools for and against anaphoric syntax constructs are pretty divided 50-50. "It" is certainly better than scala's underscore.
- Elrac 9y agoLanguage quibble: > "Function arguments are specified in brackets after the function name." No they're not! They're specified in PARENTHESES after the function name.
- hexomancer 9y agohttps://en.wikipedia.org/wiki/Bracket https://en.wikipedia.org/wiki/Bracket
- deleted 9y ago[deleted]
- rasjani 9y agoTried Kotlin few days ago. Started from "official" install docs and it said that it's just 'brew install kotlin". Yeah, it worked just fine but it would have been way nicer if they would have mentioned that you should also have Java 1.6 for compiler to actually work.
- StrykerKKD 9y agoIt's interesting how many language wants to be part of the ML family or at least look like an ML language. Kotlin has a really nice and simple syntax compared to Scala, which has way to many features.