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

Ed250c56ee7dac44d90e5fdf712314f2?s=128

David Denton

October 15, 2020
Tweet

Transcript

  1. THE KOTLIN WAY PRODUCTIVITY BY A THOUSAND CUTS… david@http4k.org @daviddenton

  2. WHO THE HELL IS THIS GUY? HI, I’M DAVID

  3. WHO THE HELL IS THIS GUY? HI, I’M DAVID ENGINEERING

    LEAD
  4. WHO THE HELL IS THIS GUY? HI, I’M DAVID ENGINEERING

    LEAD OPEN SOURCE
  5. WHO THE HELL IS THIS GUY? HI, I’M DAVID ENGINEERING

    LEAD OPEN SOURCE SPEAKER
  6. WHO THE HELL IS THIS GUY? HI, I’M DAVID ENGINEERING

    LEAD OPEN SOURCE SPEAKER TRAINER
  7. INTO THE MELTING POT “GOOD ARTISTS BORROW. GREAT ARTISTS STEAL.”

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

    GREAT ARTISTS STEAL.” - TS ELIOT
  9. - PABLO PICASSO INTO THE MELTING POT “GOOD ARTISTS BORROW.

    GREAT ARTISTS STEAL.” - TS ELIOT “IMMATURE POETS IMITATE; MATURE POETS STEAL.”
  10. INTO THE MELTING POT “GOOD LANGUAGES BORROW. GREAT LANGUAGES STEAL.”

    - ME, ABOUT KOTLIN
  11. INTO THE MELTING POT

  12. 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
  13. 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
  14. 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!)
  15. 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!)
  16. 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() }
  17. FIVE THINGS YOU’LL LOVE ABOUT KOTLIN 4. KOTLIN EVERYWHERE!

  18. 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!
  19. ONE THING ABOUT KOTLIN YOU’LL HATE 1. A WARNING YOU

    MAY NOT WANT TO GO BACK
  20. FIN #thanks! david@http4k.org @daviddenton