Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Pragmatic Kotlin Libraries: Containing Platform Types

Pragmatic Kotlin Libraries: Containing Platform Types

In this talk I present a pragmatic approach for porting Java Libraries to Kotlin without leaking platform types into the public API through a interesting case study.

Platform types destroy some of the wonderful safety guarantees that Kotlin provides. They're a necessary evil to provide nice interop with Java, but that doesn't mean we have to allow them to propogate throughout our code base.

The example we'll look at is creating a Kotlin version of the wonderful Github Scientist library. Recently, I was trying to PoC something out for a team to illustrate how Github Scientist's interesting approach to data-driven refactoring could be applied in Kotlin. Unfortunately, there wasn't a Kotlin version of Github Scientist available, so I had to create one :).

David Julia

October 14, 2016
Tweet

More Decks by David Julia

Other Decks in Programming

Transcript

  1. The Problem • We have legacy code calculating an estimated

    price for a good • The old way is slow! • We have a new faster way of calculating those prices, but we’re not sure if we’ve covered all of the edge cases
  2. Github Scientist Example require "scientist" class MyWidget def allows?(user) experiment

    = Scientist::Default.new "widget-permissions" experiment.use { model.check_user?(user).valid? } # old way experiment.try { user.can?(:read, model) } # new way experiment.run end end
  3. Github Scientist Example: An Even Nicer Version! require "scientist" class

    MyWidget include Scientist def allows?(user) science "widget-permissions" do |experiment| experiment.use { model.check_user(user).valid? } # old way experiment.try { user.can?(:read, model) } # new way end # returns the control value end end
  4. Options • Will it work in jRuby? • Find a

    Java implementation and use it • Implement your own? • Wrap a Java Library
  5. Options • Will it work in jRuby? <- Could be

    awful • Find a Java implementation and use it <- I miss Kotlin • Implement your own? <- I started down this path • Wrap a Java Library <- This is where I landed
  6. Wrapping Java Libraries = A Pragmatic Approach! • Don’t pollute

    your code with platform types • Leverage nice clean Kotlin!