• Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
  • slide 5: What started in 2010? Which codebase you're talking about? (I guess it's Kotlin and JetBrains, but it's confusing when you read a slide)
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
1,844
On Slideshare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
11
Comments
1
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. The Kotlin Programming Language
    • Dmitry Jemerov
    • JetBrains GmbH
  • 2. What is JetBrains?
  • 3. What is Kotlin?
    • Statically typed
    • JVM-targeted
    • general-purpose programming language
    • developed by JetBrains
      • intended for industrial use
    • Docs available today
    • Public beta in a few months
  • 4. Outline
    • Motivation
    • Feature Overview
    • Basic Syntax
    • Types and Classes
    • Higher-order Functions
    • Type-safe Groovy-style Builders
  • 5. Motivation
    • Started in mid 2010
    • Huge Java codebase (>50k classes, 10 years old)
    • Building tools for many nice languages
    • Want a modern, expressive language
    • … that’s easy to introduce in the existing environment
    • Need efficient tooling
    • None of the existing languages was a good fit
  • 6. Design Goals
    • Full Java interoperability
    • Compiles as fast as Java
    • More  concise  than  Java
    • Prevents more kinds of errors than  Java
    • Way  simpler  than  Scala
  • 7. Tooling
    • IntelliJ IDEA plugin developed in parallel with compiler
    • Both compiler and plugin will be released under Apache 2 license
    • Basic Eclipse plugin will be provided
  • 8. Innovation
    • Not a research project, don’t plan to publish papers
    • No ground-breaking language features
    • Focus on common sense and best practices established in other languages
      • Groovy, Scala, C#, Gosu…
    • Feature selection tradeoff: expressive yet easy to learn
  • 9. Outline
    • Motivation
    • Feature Overview
    • Basic Syntax
    • Types and Classes
    • Higher-order Functions
    • Type-safe Groovy-style Builders
  • 10. Features: Traditional
    • Properties
    • First-class functions (closures)
    • Pattern matching
    • Operator overloading
    • Local type inference
  • 11. Features: Distinguishing
    • Traits (multiple inheritance of behavior)
    • Extension functions
    • Static null-safety guarantees
    • Implicit casts after type checks
    • Inline functions (zero-overhead closures)
    • Reified generics
    • First-class delegation
    • Build infrastructure (modules) as part of the language
  • 12. Outline
    • Motivation
    • Feature Overview
    • Basic Syntax
    • Types and Classes
    • Higher-order Functions
    • Type-safe Groovy-style Builders
  • 13. Hello, world!
    • namespace hello // optional semicolons
    • // namespace-level functions
    • // types on the right
    • // no special syntax for arrays
    • fun main(args : Array<String>) : Unit {
    • println( &quot;Hello, world!&quot; )
    • }
    • fun println(message : String) {
    • // System.out is a nullable field
    • System.out?.println(message)
    • }
  • 14. Hello, <names>!
    • fun main(args : Array<String>) {
    • // type inference for local variables
    • var names = &quot;&quot;
    • for (i in args.indices) {
    • names += args[i]
    • if (i + 1 < args.size)
    • names += &quot;, &quot;
    • }
    • println( &quot;Hello, $ names !&quot; ) // string templates
    • }
    • val Array<*>.indices : Iterable<Int>
    • get () = IntRange<Int>(0, size - 1)
  • 15. Hello, <names>! (Faster version)
    • fun main(args : Array<String>) {
    • // ‘val’ means final
    • val names = StringBuilder() // no ‘new’
    • for (i in args.indices) {
    • names += args[i]
    • if (i + 1 < args.size)
    • names += &quot;, &quot;
    • }
    • println( &quot;Hello, $ names !&quot; )
    • }
    • // operator overload as an extension function
    • fun StringBuilder.plusAssign(s : String) {
    • this .append(s)
    • }
  • 16. Hello, <names>! (Realistic version)
    • fun main(args : Array<String>) {
    • val names = args.join( &quot;, &quot; )
    • println( &quot;Hello, $ names !&quot; )
    • }
  • 17. join() and forit()
    • fun <T> Iterable<T>.join(separator : String) : String {
    • val names = StringBuilder()
    • forit ( this ) {
    • names += it .next()
    • if ( it .hasNext())
    • names += separator
    • }
    • return names.toString()
    • }
    • fun <T> forit(col : Iterable<T>, f : fun (Iterator<T>) : Unit) {
    • val it = col.iterator()
    • while (it.hasNext()) {
    • f( it )
    • }
    • }
  • 18. Outline
    • Motivation
    • Feature Overview
    • Basic Syntax
    • Types and Classes
    • Higher-order Functions
    • Type-safe Groovy-style Builders
  • 19. Mapping to Java types Kotlin Java Kotlin Any Object Any? Unit void Unit Int int Int Int? Integer Int? String String String? Foo Foo Foo? Array<Foo> Foo[] Array<Foo?>? IntArray int [] IntArray? GEN LOAD
  • 20. Mapping to Java types Kotlin Java Kotlin Any Object Any? Unit void Unit Int int Int Int? Integer Int? String String String? Foo Foo Foo? Array<Foo> Foo[] Array<Foo?>? IntArray int [] IntArray? GEN LOAD
  • 21. Mapping to Java types Kotlin Java Kotlin Any Object Any? Unit void Unit Int int Int Int? Integer Int? String String String? Foo Foo Foo? Array<Foo> Foo[] Array<Foo?>? IntArray int [] IntArray? GEN LOAD
  • 22. Mapping to Java types Kotlin Java Kotlin Any Object Any? Unit void Unit Int int Int Int? Integer Int? String String String? Foo Foo Foo? Array<Foo> Foo[] Array<Foo?>? IntArray int [] IntArray? GEN LOAD
  • 23. Mapping to Java types Kotlin Java Kotlin Any Object Any? Unit void Unit Int int Int Int? Integer Int? String String String? Foo Foo Foo? Array<Foo> Foo[] Array<Foo?>? IntArray int [] IntArray? GEN LOAD
  • 24. Null-safety
    • fun parseInt(s : String) : Int? {
    • try {
    • return Integer.parseInt(s)
    • } catch (e : NumberFormatException) {
    • return null
    • }
    • }
    • fun main(args : Array<String>) {
    • val x = parseInt( &quot;123&quot; )
    • val y = parseInt( &quot;Hello&quot; )
    • print(x?.times( 2 )) // can’t say: print(x * 2)
    • if (x != null ) {
    • print(x * 2 ) // allowed after explicit check
    • }
    • }
  • 25. Implicit casts and When
    • fun foo(obj : Any?) {
    • if (obj is String) {
    • obj .substring(2)
    • }
    • when (obj) {
    • is String => obj [ 0 ]
    • is Int => obj + 1
    • !is Boolean => null
    • else => ...
    • }
    • }
  • 26. More on when-expressions
    • fun bar(x : Int) {
    • when (x) {
    • 1 , 2 , 3 => &quot;1, 2 or 3&quot;
    • -x => &quot;Zero&quot;
    • in 10 .. 100 => &quot;In range&quot;
    • !in 100 .. 1000 => &quot;Out of range&quot;
    • }
    • }
  • 27. Classes
    • open class Parent(p : Bar) {
    • open fun foo() {}
    • fun bar() {}
    • }
    • class Child(p : Bar) : Parent(p) {
    • override fun foo() {}
    • }
    • Primary constructor outside of class body
    • Constructors must initialize supertypes
    • Final by default, explicit open annotations
  • 28. Traits
    • trait T1 : Class1, OtherTrait {
      • // No state
      • fun foo() : Int = 1 // open by default
      • fun bar() : Int // abstract by default
    • }
    • class Foo(p : Bar) : Class1(p), T1, T2 { override fun bar() : Int = foo() + 1
    • }
  • 29. Disambiguation
    • trait A {
      • fun foo() : Int = 1
    • }
    • open class B() {
      • open fun foo() : Int = 2
    • }
    • class C() : B(), A {
      • override fun foo() = super <A>.foo()
    • }
  • 30. First-class Delegation
    • trait List<T> {
      • fun add(t : T)
      • fun get(index : Int) : T
    • }
    • class ListDecorator<T>(p : List<T>) : List<T> by p {
    • override fun add(t : T) {
    • log.message( &quot;Added $ t &quot; )
        • super .add(t)
    • }
      • // override fun get(index : Int) : T = super.get()
    • }
  • 31. Class objects (I)
    • Classes have no static members
    • Each class may have associated class object :
        • class Example() {
        • class object {
        • fun create() = Example()
        • }
        • }
        • val e = Example.create()
  • 32. Class objects (II)
    • Class objects can have supertypes:
        • class Example() {
        • class object : Factory<Example> {
        • override fun create() = Example()
        • }
        • }
        • val factory : Factory<Example> = Example
        • val e : Example = factory.create()
  • 33. Class objects (III)
    • Generic constraints for class objects:
        • class Lazy<T>()
        • where class object T : Factory<T>
        • {
        • private var store : T? = null
        • public val value : T
        • get () {
        • if (store == null ) {
        • store = T.create()
        • }
        • return store
        • }
        • }
  • 34. Reified generics
    • Type information is retained at runtime
      • foo is List<T>
      • Array<T>(3)
      • T.create()
    • Java types are still erased
      • foo is java.util.List<*>
  • 35. Outline
    • Motivation
    • Feature Overview
    • Basic Syntax
    • Types and Classes
    • Higher-order Functions
    • Type-safe Groovy-style Builders
  • 36. First-class functions
    • Functions
      • fun f(p : Int) : String
    • Function types
      • fun (p : Int) : String
      • fun (Int) : String
    • Function literals
      • {p => p.toString() }
      • {(p : Int) => p.toString() }
      • {(p : Int) : String => p.toString() }
  • 37. Higher-order functions
    • fun <T> filter( c : Iterable<T>, f : fun (T) : Boolean) : Iterable<T>
    • filter(list, {s => s.length < 3})
  • 38. Syntax sugar
    • Last function literal argument
      • filter(list) {s => s.length < 3 }
    • One-parameter function literal
      • filter(list) { it .length < 3 }
    • Infix calls
      • a contains b // same as a.contains(b)
  • 39. “ LINQ” style syntax
    • users
    • .filter { it hasPrivilege WRITE }
    • .map { it => it.fullName }
    • .orderBy { lastName }
  • 40. Lock example (I)
    • myLock.lock()
    • try {
      • // Do something
    • }
    • finally {
      • myLock.unlock()
    • }
  • 41. Lock example (II)
    • lock(myLock) {
      • // Do something
    • }
    • fun lock(l : Lock, body : fun () : Unit)
  • 42. Lock example (III)
    • inline fun lock(l : Lock, body : fun () : Unit) {
      • l.lock()
      • try {
        • body()
      • }
      • finally {
        • l.unlock()
      • }
    • }
  • 43. Extension functions
    • Functions
      • fun Foo.f(p : Int) : String
    • Function types
      • fun Foo.(p : Int) : String
      • fun Foo.(Int) : String
    • Function literals
      • {Foo.(p : Int) => this .toString() }
      • {Foo.(p : Int) : String => this .toString() }
      • {Foo.(p : Int) : String => toString() }
  • 44. Outline
    • Motivation
    • Feature Overview
    • Basic Syntax
    • Types and Classes
    • Higher-order Functions
    • Type-safe Groovy-style Builders
  • 45. Builders in Groovy
    • html {
    • head {
    • title &quot;XML encoding with Groovy&quot;
    • }
    • body {
    • h1 &quot;XML encoding with Groovy&quot;
    • p &quot;this format can be used as an alternative to XML&quot;
    • /* an element with attributes and text content */
    • ahref: 'http://groovy.codehaus.org' [ &quot;Groovy&quot; ]
    • }
    • }
  • 46. Builders in Kotlin
    • html {
    • head {
    • title { + &quot;XML encoding with Kotlin&quot; }
    • }
    • body {
    • h1 { + &quot;XML encoding with Kotlin&quot; }
    • p { + &quot;this format is now type-safe &quot; }
    • /* an element with attributes and text content */
    • a(href= &quot;http://jetbrains.com/kotlin&quot; ) { + &quot;Kotlin&quot; }
    • }
    • }
  • 47. Builders: Implementation (I)
    • Function definition fun html(init : fun HTML.() : Unit) : HTML { val html = HTML() html. init() return html }
    • Usage html { this .head { ... } }
  • 48. Builders: Implementation (II)
    • Function definition fun html(init : fun HTML.() : Unit) : HTML { val html = HTML() html. init() return html }
    • Usage html { head { ... } }
  • 49. Builders: Implementation (III)
    • abstract class Tag( val name : String) : Element {
    • val children = ArrayList<Element>()
    • val attributes = HashMap<String, String>()
    • }
    • abstract class TagWithText(name : String) : Tag(name) {
    • fun String.plus() {
    • children.add(TextElement( this ))
    • }
    • }
    • class HTML() : Tag( &quot;html&quot; ) {
    • fun head(init : fun Head.() : Unit) { }
    • fun body(init : fun Body.() : Unit) { }
    • }
  • 50. Builders in Kotlin
    • html {
    • head {
    • title { + &quot;XML encoding with Kotlin&quot; }
    • }
    • body {
    • h1 { + &quot;XML encoding with Kotlin&quot; }
    • p { + &quot;this format is now type-safe &quot; }
    • /* an element with attributes and text content */
    • a(href= &quot;http://jetbrains.com/kotlin&quot; ) { + &quot;Kotlin&quot; }
    • }
    • }
  • 51. Resources
    • Documentation: http://jetbrains.com/kotlin
    • Blog: http://blog.jetbrains.com/kotlin
    • Issue tracker: http://youtrack.jetbrains.net/issues/KT
    • Twitter:
      • @project_kotlin
      • @intelliyole
  • 52. Q&A Dmitry Jemerov [email_address]