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

Productivity by a thousand cuts: The Kotlin Way

Productivity by a thousand cuts: The Kotlin Way

David Denton

October 15, 2020
Tweet

More Decks by David Denton

Other Decks in Programming

Transcript

  1. - PABLO PICASSO INTO THE MELTING POT “GOOD ARTISTS BORROW.

    GREAT ARTISTS STEAL.” - TS ELIOT “IMMATURE POETS IMITATE; MATURE POETS STEAL.”
  2. val myValue: Int = null operation(myValue.toString()) FIVE THINGS YOU’LL LOVE

    ABOUT KOTLIN 1. NULL SAFETY ▸ Nulls: The Billion Dollar Mistake ▸ Kotlin builds null safety into the type system ▸ Hard to subvert it in your own code
  3. val myValue: Int = null operation(myValue.toString()) val myValue: Int =

    null operation(myValue.toString()) val myValue: Int? = null operation(myValue?.toString() ?: “default”) FIVE THINGS YOU’LL LOVE ABOUT KOTLIN 1. NULL SAFETY ▸ Nulls: The Billion Dollar Mistake ▸ Kotlin builds null safety into the type system ▸ Hard to subvert it in your own code
  4. fun File.readText() = InputStreamReader( FileInputStream(this)) .use { StringWriter() .apply {

    it.copyTo(this)}.toString() } val text = File("content").readText() FIVE THINGS YOU’LL LOVE ABOUT KOTLIN 2. EXTENSION FUNCTIONS ▸ Extend types you don’t own! (And ones you do!)
  5. fun File.readText() = InputStreamReader( FileInputStream(this)) .use { StringWriter() .apply {

    it.copyTo(this)}.toString() } val text = File("content").readText() fun File.readText() = InputStreamReader( FileInputStream(this)) .use { StringWriter() .apply { it.copyTo(this)}.toString() } val text = File("content").readText() FIVE THINGS YOU’LL LOVE ABOUT KOTLIN 2. EXTENSION FUNCTIONS ▸ Extend types you don’t own! (And ones you do!)
  6. FIVE THINGS YOU’LL LOVE ABOUT KOTLIN 3. SENSIBLE CONCISENESS ▸

    Immutable by default ▸ Type inference ▸ Data classes ▸ Trailing Lambdas ▸ Infix functions & Operators ▸ DSLs fun ChatApp(): PolyHandler { val userCounter = AtomicInteger() val messages = mutableListOf<String>() val participants = ConcurrentHashMap<String, Websocket>() fun addMessage(new: String) { messages.add(new) participants.values.forEach { it.send(WsMessage(new)) } } fun newConnection(ws: Websocket) { val id = "user${userCounter.incrementAndGet()}" participants += id to ws messages.map(::WsMessage).forEach { ws.send(it) } addMessage("$id joined") ws.onMessage { addMessage("$id: ${it.bodyString()}") } ws.onClose { participants -= id addMessage("$id left") } } val http = BasicAuth("http4k", Credentials("kotlin", “isgreat")) .then(static(Classpath())) val ws = websockets("/ws" bind ::newConnection) return PolyHandler(http, ws) } fun main() { ChatApp().asServer(Jetty(5000)).start() }
  7. FIVE THINGS ABOUT KOTLIN YOU’LL LOVE 5. THE 100S OF

    OTHER THINGS, INCLUDING… ▸ Really easy to learn ▸ Idea automatically converts Java to Kotlin! ▸ Awesome standard library ▸ Language is Open Source ▸ Seamless Java Interop ▸ Friendly community ▸ Both Developers and OSS maintainers absolutely love Kotlin!