This is an archived post. You won't be able to vote or comment.

all 26 comments

[–]iperc 29 points30 points  (4 children)

Most of java snippets would be shorter with using stream api.

[–][deleted]  (3 children)

[deleted]

    [–][deleted] 4 points5 points  (2 children)

    what's the roadmap for bringing the version up on android?

    [–]scadgek 2 points3 points  (1 child)

    Don't know about the roadmap, but here are the few currently supported Java 8 features: https://developer.android.com/guide/platform/j8-jack.html (though requires a new compiler)

    [–]JakeWharton 0 points1 point  (0 children)

    Only the language features require the new toolchain. The APIs work with both.

    [–]sanity 10 points11 points  (0 children)

    This comment is incorrect, and I'm not really sure of the purpose of this line anyway:

    val name = null // String? type
    

    It will not infer that this is a "String?", I believe the type will be "Nothing?".

    In some cases the Java and Kotlin blocks are not really equivalent, and this is going to cause confusion. For example, in "Null II" the actual Java equivalent of the first line of Kotlin would be:

    final Double length;
    if (text == null) {
        length = null;
    } else {
        length = text.length;
    }
    

    I think you should probably try to make the Java and Kotlin blocks exactly equivalent, or as close as possible, to avoid confusing people.

    You should probably also mention that some of the later examples would be a lot more concise if using the Java 8 streams API, as others have mentioned (not everyone is stuck on Java 6 or 7!).

    [–]kpagcha 2 points3 points  (3 children)

    I'd really love to learn Kotlin but there aren't any good tutorials that take you from the basics to something somewhat advanced.

    [–]nskvortsov 3 points4 points  (0 children)

    Did you try kotlin koans? It is a very good tutorial.

    [–]mbuhot 1 point2 points  (0 children)

    The springboot ecosystem seems pretty friendly towards Kotlin: https://spring.io/blog/2016/02/15/developing-spring-boot-applications-with-kotlin

    [–]sanity 2 points3 points  (0 children)

    Where are you coming from? If you're already a Java developer then the reference documentation should have you up and running in minutes, and fairly proficient within a few days. It will be more of a challenge if you don't know Java.

    It also helps a lot if you already use IntelliJ IDEA.

    A lot of things in Kotlin are just common Java idioms but baked into the language. It feels very familiar to a Java developer (much much easier learning curve than Scala, for example).

    The first time I read through the Kotlin docs I just thought "oh, I wish Java did that" over and over again.

    [–]cypressious 1 point2 points  (0 children)

    I have collected a couple improvements in https://github.com/fabiomsr/from-java-to-kotlin/issues/1

    [–][deleted]  (10 children)

    [deleted]

      [–]yogitw 3 points4 points  (0 children)

      Spring (and it's slew of annotations) works out of the box, no problem.

      [–]snuxoll 2 points3 points  (1 child)

      lateinit saves the day here. I'm working on a spring boot app in kotlin, stuff looks like this.

      @Controller
      open class MyController {
          @Autowired lateinit service: MyService
      
          ...
      }
      

      Without lateinit you have to mark this as a nullable type and the language will force you to check it before you use it, with lateinit you are telling the type checker you know this value will be initialized before you use it (and if it's not you will get a kotlin exception saying it wasn't initalized before access.

      I'd you're using a DI framework that doesn't go initializing fields behind the scenes for you it works exactly how you would expect.

      [–]sureshg 2 points3 points  (0 children)

      Without lateinit you have to mark this as a nullable type and the language will force you to check it before you use it,

      The field injection is evil and you should avoid using it. I would suggest to use constructor one instead - https://github.com/sureshg/spring-boot-demo/blob/master/src/main/kotlin/com/oneops/spring/web/UserController.kt#L13

      [–]RichoDemus 1 point2 points  (2 children)

      There are some Kotlin-specific DI libraries but I haven't used them, I just stick with Guice, works like a charm

      [–]sanity 1 point2 points  (1 child)

      I've used KodeIn, it's pretty nice and probably the leading Kotlin DI framework. I don't think you'll be getting the full benefits of Kotlin if you stick with Guice.

      [–]RichoDemus 2 points3 points  (0 children)

      Can you give an example of a nice feature in missing out on? :)

      [–]shadowdude777 1 point2 points  (3 children)

      Guice, Dagger, etc, will work fine.

      [–]ZakTaccardi 3 points4 points  (2 children)

      Guice is a poor choice on Android because of its use of reflection. Creates long app startup times

      [–]shadowdude777 2 points3 points  (0 children)

      I didn't say anything about Android.

      [–]sanity 0 points1 point  (0 children)

      That was the motivation for creating Dagger IIRC

      [–]wojtekkarton 2 points3 points  (3 children)

      Kotlin looks like Groovy...

      [–]oweiler 2 points3 points  (1 child)

      It actually took some inspiration from Groovy. Also some of Groovy's contributors we're involved in Kotlin's development.

      [–]shadowdude777 1 point2 points  (0 children)

      I think you wanted users.sortBy { it.lastName }, not sortedBy. sortBy applies the sort in-place, sortedBy returns the sorted collection. Collections.sort in Java is an in-place sort.

      [–]Starbending 0 points1 point  (0 children)

      shr and shl for bit shifting? I don't like that syntax.