Kotlin: The Next Frontier for Modern (Meta)Programming

209bed5a8a467a07a397b25c00a26e9e?s=47 mvndy_hd
October 04, 2018

Kotlin: The Next Frontier for Modern (Meta)Programming

With growing complexity in our software systems, technology companies now dedicate greater effort to design product infrastructure to ensure modularity, low-cost solutions, and scalability. Object Oriented Programming (OOP)'s tendency of designing object-models for domain solutions in addition to its merits in handling large data and reducing complexity makes its usage a natural choice in the industry. Despite universal usage, Java's limitations in language techniques fail to capture all domain design decisions in a cohesive manner. This issue describes aspects of relational domains, in which scattering and tangling between mapped sources and targets forces large-scale projects to sacrifice domains that guarantees quality software. Kotlin's open-source community is active, its uses are ever-expanding, and its future is bright. This session intends to analyze a general definition of crosscutting and how it affects growing software, how Aspect-Oriented Programming (AOP) attempts to tackle crosscutting, shortcomings in Java’s current approaches in reflexive programming, and subsequently discuss my experiences with TornadoFX in solving day-to-day business inefficiencies through my first attempt at metaprogramming. Exploration through examples will provide insight in Kotlin’s approaches to metaprogramming intended to expose Kotln's predisposition to AOP. By harnessing the power of Kotlin, we can make strides in creating a foundation for rational framework for metaprogramming.



October 04, 2018


  1. Kotlin: The Next Frontier for Modern (Meta) Programming Amanda Hinchman-Dominguez

    @hinchman_amanda Kotlin, TornadoFX and Metaprogramming
  2. Thomas Nield

  3. TornadoFX a JavaFX Framework written in Kotlin

  4. Characteristics of Functional Languages Functions are treated as first class

    citizens Functions have no side-effects Metaprogramming capabilities
  5. Metaprogramming is magic!! • Programs that treat programs as data

    – read, analyze, or transform other programs • Metaprogramming can be used to make our lives easier
  6. None
  7. Primitive Filters remove(a, b) && ! or(a, b) )= ||

    ( ( )= topEdge(a) )= shiftUp( == ) !( Piq: cesarloose
  8. A READABLE Horizontal Edge

  9. A MEMORY-OPTIMAL Horizontal Edge

  10. Crosscutting = (Scattering && Tangling) • Scattering – when a

    source element is related to multiple target elements • Tangling – when a target element is related to multiple source elements • Crosscutting occurs if there exists both scattering & tangling • “spaghetti code” sources targets
  11. Metaprogramming A solution to crosscutting • We sacrifice one domain

    design decision for another • Metaprogramming comes in different forms • Wizards - “Monkey patching” • Aspect-Oriented Programming (AOP) • Domain Specific Languages (DSLs) Flickr: Susan Lucas Hhoffman
  12. None
  13. None
  14. Aspect-Oriented Programming (AOP) • AOP is how Java approaches crosscutting

    • Draws out a higher level of abstraction to act as a weaver between concerns Flickr: Chris Radcliffe
  15. Memoization - optimization technique

  16. Java Annotations API

  17. How does Java achieve AOP? 1. Declare the @interface 2.

    Specify the @Retention and its targets 3. Add elements
  18. How does Java achieve AOP? • Dynamic introspection prime for

    reflection • Runtime annotation carries through to the JVM
  19. Current Shortcomings in Aspect-Oriented Programming • Annotations are only a

    form of metadata • No direct effect on the annotated code • Annotation processing occurs only at designated annotations • No dynamic generation for elements • True metaprogramming doesn’t play nicely with static types • Generating code without compiling risks type-checking • True metaprogramming does not respect encapsulation • Statically-typed languages overcomplicate
  20. Imperative v. Declarative Programming Salting your food • An imperative

    approach (HOW): I see the salt is out of my reach. If I reach over the beans and the sauce and avoiding knocking the drinks over I could probably get it without brushing over my own food. • A declarative approach (WHAT): Pass the salt please. • Execution is more efficient • Reduced bugs Flickr: Joe King
  21. Domain-Specific Languages (DSLs) • Regular DSLs • Cares about a

    specific concern and nothing else • Not a general purpose language & difficult to combine with • Kotlin creates internal DSLs • Uses its own language • Retains key advantages of DSLs
  22. Type-Safe CSS • Blocks, or { }, can be passed

    when the terminal parameter is a function op -> () • Type-safe builders allows Kotlin-based DSLs to create complex hierarchies in a declarative way
  23. Higher Order Functions and Lambdas Function as a Parameter

  24. Function as a Parameter Parameter types Return type Receiver type

    Higher Order Functions and Lambdas
  25. Parameter types Return type Receiver type Higher Order Functions and

    Lambdas A.(B) C or (A, B) C Function as a Parameter
  26. Reified Generics • Function bytecode copies into every place where

    the function is called • When the function is called the compiler modifies the generated bytecode to use the corresponding class directly • Can be used • Type checks/casts • Use the Kotlin reflection API • As a type argument to call other functions
  27. Scopes • simple constructs used to pass objects like models

    to other classes • Makes the View/Controller unique to a smaller subset of instances in an application
  28. Passing Scopes to Components

  29. Type parameter used in receiver and return types Generic-Class-as-a-Parameter Pattern

    Type parameter declaration – any Component may be passed
  30. Passing Scopes with Reified Generics “reified” declares that this type

    parameter will not be erased at runtime
  31. Passing Scopes to Components

  32. • Metaprogramming is a solution to crosscutting • Common forms

    of metaprogramming has their shortcomings • TornadoFX leverages functional Kotlin features to streamline JavaFX The Takeaway
  33. None

  35. • True reflection does not respect encapsulation • Generating code

    at runtime means no compiler for type- checking Challenges w/ Statically-Typed Metaprogramming
  36. None
  37. Special Thanks Carl Walker @bekwaminc Edvin Syse @edvinsyse Thomas Neild

    @thomasneild9727 Ruckus T Boom @ruckustboom • #TornadoFX channel on Slack • Kotlin community • KotlinConf Committee • Ebad Ahmadzadeh
  38. Noted Projects: • TornadoFX - https://github.com/edvin/tornadofx • TilesFX - https://github.com/HanSolo/tilesfx

    • TornadoFX-DnD-TilesFX – https://github.com/ahinchman1/TornadoFX-DnD-TilesFX • Neighborhood Cat Scheduler – https://github.com/Kotlin- Thursdays/Neighborhood-Cat-Scheduler • TornadoFX-Suite - https://github.com/ahinchman1/TornadoFX-Suite • Kastree - https://github.com/cretz/kastree