Practical Functional Java

Extra: Kotlin

Jeff Butler

github.com/jeffgbutler/practical-functional-java

Kotlin

Same function, Less Ceremony

Short Kotlin Overview

  • Created by JetBrains
  • An Official Language for Android Development
  • An Official Language for Spring Development
  • Easy Interop with Java
  • Includes all this functional stuff we've learned
  • Can Run on Java 6!

Three Important Things

  1. Immutability First
  2. Null Safety
  3. Extension Functions

These three things alone reduce quite a lot of the boilerplate code we've just been through

Immutability, Null Safety

Java

            
  public class Person {
    private String firstName;
    private String lastName;
                  
    private Person(Builder builder) {
      firstName = Objects.requireNonNull(builder.firstName);
      lastName = Objects.requireNonNull(builder.lastName);
      this.description = description;
    }
    // getters, toString, equals, etc.
    public static class Builder {
      private String firstName;
      private String lastName;

      public Builder withFirstName(String firstName) {
        this.firstName = firstName;
        return this;
      }
      // etc...
    }
  }
            
          

More on Null Safety

Every Java reference can be null. This requires a lot of null checking or Optional wrapping.

            
  public class Person {
    private Address mailingAddress;
    public Address getMailingAddress() {
      return mailingAddress;
    }
  }

  public class Address {
    private String secondAddresLine;
    public String getSecondAddressLine() {
      return secondAddresLine;
    }
  }
            
          

Null Safety in Kotlin

Kotlin references can only be null if specifically declared to allow null

            
  data class Person (val mailingAddress: Address?)

  data class Address (val secondAddressLine: String?)
            
          
  • Declare a null capable type with a following ?
  • The ?. operator is null safe navigation
  • The ?: operator (Elvis) is null coalescing

The Visitor Pattern Revisited

In essence, the visitor allows adding new virtual functions to a family of classes, without modifying the classes. (wikipedia)

In the XML renderer we used the visitor pattern to add a "render" method to each of the XML model classes

Visitor is a very clever pattern, but hard to grasp

Kotlin Extension Functions

Same outcome as visitor, but far simpler to understand

            
data class Attribute (val name: String, val value: String)

// declare a "render" extension function
// extension functions can be in different packages,
// different JARs, different projects
fun Attribute.render(): String {
    // extension methods have access non-private
    // attributes and methods

    // this is a Kotlin string template
    return """$name="$value" """
}
            
          

Thank You!

Please rate the session. Suggestions for improvements are very welcome.

jeffgbutler@gmail.com

github.com/jeffgbutler