• Save
Kotlin gets Reflection
Upcoming SlideShare
Loading in...5
×
 

Kotlin gets Reflection

on

  • 1,826 views

Presented at JVM Language Summit, 2013

Presented at JVM Language Summit, 2013

Statistics

Views

Total Views
1,826
Slideshare-icon Views on SlideShare
1,808
Embed Views
18

Actions

Likes
1
Downloads
0
Comments
0

1 Embed 18

http://www.slideee.com 18

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Kotlin gets Reflection Kotlin gets Reflection Presentation Transcript

    • KOTLIN GETS REFLECTION Andrey.Breslav@JetBrains.com
    • Introspection is examination of one's own conscious thoughts and feelings. Schultz, D. P.; Schultz, S. E. (2012). A history of modern psychology (10th ed.)
    • LEGAL Takeiteasy
    • DON’T JUDGE STRICTLY
    • OUTLINE • Intro • Ways of Introspection • Reflection API • Reflection Literals • Expression Trees • Conclusion
    • USE CASES Dependency Injection Data binding Convention over configuration Hacks Workarounds Black Magic
    • INTROSPECTION Instances - what is your class? Classes & Types - what are your members? supertypes? create an instance Methods/Fields - what are your parameters/types/etc? run with these args. Expressions - ???
    • JAVA.LANG.REFLECT? Top-level functions Default arguments Properties Nullable types Special types (Nothing, (Mutable)List, etc) … + modules (classes in a package)
    • METADATA *.kt *.class (kotlin) *.jar (kotlin) kotlin symbols kotlin symbols kotlin symbols java symbols kotlinc
    • HOW TO STORE METADATA? *.class Custom attributes Annotations Annotate each symbol One big annotation
    • ONE BIG ANNOTATION *.class @KotlinClass(“data”) val/var types defaults … Java definitions erased generic annotations …
    • RE-USE j.l.Class *.kt *.class AST @KotlinClass Symbols
    • DISCREPANCY 1 java.lang.annotation.Annotation vs org.jetbrains.kotlin.internal….AnnotationDescriptor
    • PURE JAVA CLASSES? j.l.Class *.kt *.class AST @KotlinClass Symbols *.java java2k
    • DISCREPANCY 2 // Java class Foo { @NotNull String getFirstName() { … } String getMiddleName() { … } } // Corresponding Kotlin class Foo { fun getFirstName(): String fun getMiddleName(): String? }
    • DISCREPANCY 2 j.l.Class *.class Symbols *.java java2k annotations.xml
    • SUMMARY 1 Kotlin-specific reflection API - works for Java as well Metadata representation - one big annotation - re-use code from the compiler Problems - representing annotations - nullable/mutable types
    • ENTRY POINTS
    • SYNTAX (TENTATIVE) Foo::class expr::class List<String>::member org.sample.bar::member expr::member expr::type (maybe)
    • USE CASES: PASSING CODE AROUND list.filter(Item::isValid) * Why not a lambda? foo {a, b, c -> bar(a, b, c) }
    • USE CASES: CONSTRUCTORS fun <T: Tag> tag( create: () -> T, init: T.() -> Unit ) { … } tag(::DIV) { // kara.tags::DIV … }
    • USE CASES: DATA BINDING class Model { var userName: String by observable() } bind(view.textField, model::userName)
    • GENERICS? fun foo(s: String): Foo ::foo : (String) -> Foo fun <T> foo(t: T): T { … } ::foo : ∀T.(T) -> T
    • RANK-2 POLYMORPHISM interface Function1<P1, R> { R invoke(P1 p1); } vs interface GenericFunction1_1<P1, R> { <T> R<T> invoke(P1<T> p1); }
    • ENCODING FOR FUNCTIONS f: (Foo) -> Bar is Function1<Foo, Bar> interface Function1<P1, R> { R invoke(P1 p1); }
    • GENERIC FUNCTIONS f: <T>(List<T>) -> T • GenericFunction<List<T>, T> ??? • GenericFunction<T, List<T>, T> ??? interface GenericFunction1_1<P1, R> { <T> R<T> invoke(P1<T> p1); } + Kinds
    • GENERICS class Foo<T> { fun <R> bar(t: T): R { … } } Foo<T’>::bar<R’> : (T’) -> R’
    • EXPR::TYPE val x: Any = listOf(1, 2, 3) x::class -> java.util.ArrayList x::type -> java.util.ArrayList<Unknown> vs val x = listOf(1, 2, 3) // x: List<Int> x::class -> java.util.ArrayList x::type -> java.util.ArrayList<Int>
    • DELEGATED PROPERTIES val foos: List<Foo> by Lazy { foos.find(…) } class Lazy<T>(compute: () -> T) { private var value: T? = null fun get(me: Any, p: PropertyMetadata): T { if (value == null) value = compute() return value } }
    • DELEGATED PROPERTIES val foos: List<Foo> by Lazy { foos.find(…) } ::foos::delegate : Lazy<Foo> ::foos : Property<List<Foo>> or ::foos.delegate : Lazy<Foo> ::foos : DelegatedProperty< List<Foo>, Lazy<Foo> >
    • SUMMARY 2 Reflection literals - Static name lookup & typing - Generics are hard, as usual
    • CODE INTROSPECTION I told you!
    • USE CASES: LINQ db .selectFrom(::User) .where { lastName.startsWith(“A”) } .orderBy { lastName + firstName }
    • server client server USE CASES: WEB html { body { onLoad { ... } ... } }
    • EXPRESSION TREES fun onLoad(ast: Expression<() -> Unit>) { ... } onLoad { ... // compiled to factory calls } At run time the tree can be translated to JS
    • SUMMARY Kotlin’s introspection facilities - Reflection API - Java interop is the hardest issue - Reflection literals - Issues with generics - Expression trees - Compiler as a service