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

Kotlin: The Next Frontier for Modern (Meta)Prog...

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.

mvndy_hd

October 04, 2018
Tweet

More Decks by mvndy_hd

Other Decks in Programming

Transcript

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

    @hinchman_amanda Kotlin, TornadoFX and Metaprogramming
  2. Characteristics of Functional Languages Functions are treated as first class

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

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

    ( ( )= topEdge(a) )= shiftUp( == ) !( Piq: cesarloose
  5. 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
  6. 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
  7. 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
  8. How does Java achieve AOP? 1. Declare the @interface 2.

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

    reflection • Runtime annotation carries through to the JVM
  10. 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
  11. 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
  12. 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
  13. 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
  14. Parameter types Return type Receiver type Higher Order Functions and

    Lambdas A.(B) C or (A, B) C Function as a Parameter
  15. 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
  16. 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
  17. Type parameter used in receiver and return types Generic-Class-as-a-Parameter Pattern

    Type parameter declaration – any Component may be passed
  18. • Metaprogramming is a solution to crosscutting • Common forms

    of metaprogramming has their shortcomings • TornadoFX leverages functional Kotlin features to streamline JavaFX The Takeaway
  19. • True reflection does not respect encapsulation • Generating code

    at runtime means no compiler for type- checking Challenges w/ Statically-Typed Metaprogramming
  20. 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
  21. 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