Kotlin: The Next Frontier for Modern (Meta) Programming Kotlin, - - PowerPoint PPT Presentation

kotlin the next frontier for modern meta programming
SMART_READER_LITE
LIVE PREVIEW

Kotlin: The Next Frontier for Modern (Meta) Programming Kotlin, - - PowerPoint PPT Presentation

Kotlin: The Next Frontier for Modern (Meta) Programming Kotlin, TornadoFX and Metaprogramming Amanda Hinchman-Dominguez @hinchman_amanda Thomas Nield TornadoFX a JavaFX Framework written in Kotlin Characteristics of Functional Languages


slide-1
SLIDE 1

Kotlin: The Next Frontier for Modern (Meta) Programming

Amanda Hinchman-Dominguez @hinchman_amanda Kotlin, TornadoFX and Metaprogramming

slide-2
SLIDE 2

Thomas Nield

slide-3
SLIDE 3

TornadoFX


a JavaFX Framework written in Kotlin

slide-4
SLIDE 4

Characteristics of Functional Languages

Functions are treated as first class citizens Functions have no side-effects Metaprogrammin g capabilities

slide-5
SLIDE 5

Metaprogramming is magic!!

  • Programs that treat programs as data – read, analyze,
  • r transform other programs
  • Metaprogramming can be used to make our lives easier
slide-6
SLIDE 6
slide-7
SLIDE 7

Primitive Filters

remove(a, b)

&& !

  • r(a, b)

)=

||

( ( )=

topEdge(a)

)=

shiftUp(

==

)

!(

Piq: cesarloose

slide-8
SLIDE 8

A READABLE Horizontal Edge

slide-9
SLIDE 9

A MEMORY-OPTIMAL Horizontal Edge

slide-10
SLIDE 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

slide-11
SLIDE 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

slide-12
SLIDE 12
slide-13
SLIDE 13
slide-14
SLIDE 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

slide-15
SLIDE 15

Memoization - optimization technique

slide-16
SLIDE 16

Java Annotations API

slide-17
SLIDE 17

How does Java achieve AOP?

  • 1. Declare the

@interface

  • 2. Specify the

@Retention and its targets

  • 3. Add elements
slide-18
SLIDE 18

How does Java achieve AOP?

  • Dynamic introspection

prime for reflection

  • Runtime annotation

carries through to the JVM

slide-19
SLIDE 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
slide-20
SLIDE 20

Imperative v. Declarative Programming


Salting your food

  • An imperative approach (HOW): I see

the salt is out of my reach. If I reach

  • ver the beans and the sauce and

avoiding knocking the drinks over I could probably get it without brushing over my

  • wn food.
  • A declarative approach (WHAT): Pass

the salt please.

  • Execution is more efficient
  • Reduced bugs

Flickr: Joe King

slide-21
SLIDE 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
slide-22
SLIDE 22

Type-Safe CSS

  • Blocks, or { }, can be

passed when the terminal parameter is a function

  • p -> ()
  • Type-safe builders allows

Kotlin-based DSLs to create complex hierarchies in a declarative way

slide-23
SLIDE 23

Higher Order Functions and Lambdas

Function as a Parameter

slide-24
SLIDE 24

Function as a Parameter Parameter types Return type Receiver type

Higher Order Functions and Lambdas

slide-25
SLIDE 25

Parameter types Return type Receiver type

Higher Order Functions and Lambdas

A.(B) C

  • r

(A, B) C

Function as a Parameter

slide-26
SLIDE 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
slide-27
SLIDE 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

slide-28
SLIDE 28

Passing Scopes to Components

slide-29
SLIDE 29

Type parameter used in receiver and return types

Generic-Class-as-a-Parameter Pattern

Type parameter declaration – any Component may be passed

slide-30
SLIDE 30

Passing Scopes with Reified Generics

“reified” declares that this type parameter will not be erased at runtime

slide-31
SLIDE 31

Passing Scopes to Components

slide-32
SLIDE 32
  • Metaprogramming is a solution to crosscutting
  • Common forms of metaprogramming has their shortcomings
  • TornadoFX leverages functional Kotlin features to streamline

JavaFX

The Takeaway

slide-33
SLIDE 33
slide-34
SLIDE 34

TORNADO FX SUITE

slide-35
SLIDE 35
  • True reflection does not respect encapsulation
  • Generating code at runtime means no compiler for

type-checking

Challenges w/ Statically-Typed Metaprogramming

slide-36
SLIDE 36
slide-37
SLIDE 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