Saturday, June 24, 2017

Kotlin: Getting Started - Part 1

I've been using Kotlin for a month or so now, and here are some of the smaller things that I found very useful.

Some Basic Patterns

Here are some basic patterns that you probably (hopefully) use in Java, and how to do them with Kotlin.

Builder/Constructor

In Java you may have classes with many overloaded constructors, or with constructors that have lots of nullable parameters.   I think we can all agree that this is just all around tedious for both the producer of the class and the consumer / caller.   You might decide to make a builder, which allows you to have a single constructor, and have immutable fields in your target class.   While this can make things a bit easier for the consumer/caller,  it's still a lot of boilerplate code.

 Kotlin provides with two features that can help with this in some cases:
  1. Optional parameters with default values
  2. Named parameters
Of course, you can still make a builder if you want.  Also, you can define secondary constructors.

Caveat for Java integration: These features are not available for Java code calling Kotlin, so don't expect this to magically improve Java.

Memoize

It's really easy to create an 'initialize once' field in Kotlin: just use: as lazy { ... }


You can chain these together, due to the fact that the 'as lazy' properties act just like normal properties.



Integrating with Existing Java Code

If you're considering Kotlin, but you have a bunch of existing Java code you might be concerned about using Kotlin into your code base.   Fortunately, Kotlin support is really easy to add to your build, and it's very easy to interoperate between Kotlin and Java.

Adding Kotlin Support - Gradle

To add Kotlin compilation support to an existing Gradle build.


  1. Put the Kotlin plugins in the buildscript class path:

    buildscript {
        ext {
            kotlinVersion = '1.1.2-4'
        }
        repositories {
            jcenter()
            maven { url "https://plugins.gradle.org/m2/" }
        }
    
        dependencies {    // Gradle Plugin classpath.
            classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
            classpath("org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}")
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}"
        }
    }
    

    NOTE: I'm avoiding the newer Gradle plugin configuration syntax because it does not support string interpolation. You have to repeat the kotlinVersion information over and over.
  2. Enable the Kotlin plugins:
    
    
        apply plugin: 'kotlin'
        apply plugin: 'kotlin-spring'
    
  3. 
    
  4. Set the target JVM (optional, but I prefer to do this):

        compileKotlin {
            kotlinOptions.jvmTarget = "1.8"
        }
    
        compileTestKotlin {
            kotlinOptions.jvmTarget = "1.8"
        }
    

  5. Add the Kotlin runtime library dependencies:

        dependencies { 
            // Kotlin/JVM libraries
            compile("org.jetbrains.kotlin:kotlin-stdlib:${kotlinVersion}")
            compile("org.jetbrains.kotlin:kotlin-stdlib-jre8:${kotlinVersion}")
            compile("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
            // Kotlin SLF4J utility
            compile 'io.github.microutils:kotlin-logging:1.4.4'
        }
    

Calling Kotlin Functions from Java


Java will see Kotlin functions as static methods in a class named like this: <package><KotlinFileName>Kt.   Basically, just the class name you might expect, plus 'Kt' on the end.



No comments:

Post a Comment