The Kotlin Programming Language

Slides:



Advertisements
Similar presentations
Language Features for JDK7 Neal Gafter Joshua Bloch Google.
Advertisements

7-Jun-14 Lists. Arrays and Lists Arrays are a fixed length and occupy sequential locations in memory This makes random access (for example, getting the.
KAnnotator: Inferring Type Information from Java Byte Codes Alexey Sedunov Andrey Breslav Svetlana Isakova Evgeny Gerashchenko JetBrains, s. r. o
Sequence of characters Generalized form Expresses Pattern of strings in a Generalized notation.
XML: Extensible Markup Language
1 Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 13.
Java Script Session1 INTRODUCTION.
CS162 Week 2 Kyle Dewey. Overview Continuation of Scala Assignment 1 wrap-up Assignment 2a.
1 ADT and Data Structure Example Generics / Parameterized Classes Using a Set Implementing a Set with an Array Example: SetADT interface Example: ArraySet.
CSE341: Programming Languages Lecture 27 Generics vs. Subtyping; Bounded Polymorphism Dan Grossman Fall 2011.
Map and Fold Building Powerful Abstractions. Hello. I’m Zach, one of Sorin’s students.
CSE341: Programming Languages Lecture 11 Closures-ish Java & C Dan Grossman Fall 2011.
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
HOT Programming in Java COS 441 Princeton University Fall 2004.
Programming in Scala Chapter 1. Scala: both object-oriented and functional Scala blends –object-oriented and –functional programming in a –statically.
Type Inference: CIS Seminar, 11/3/2009 Type inference: Inside the Type Checker. A presentation by: Daniel Tuck.
UNIT II Decision Making And Branching Decision Making And Looping
Kotlin Workshop: Classes, Inheritance and more… Andrey Breslav Dmitry Jemerov.
Computer Science 209 The Strategy Pattern II: Emulating Higher-Order Functions.
Topic 3 The Stack ADT.
Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators.
1 Extending Java And Developing DSLs With Open Source Language Workbench JetBrains MPS Konstantin Solomatov JetBrains Lead Developer for JetBrains MPS.
The Scala Programming Language presented by Donna Malayeri.
A brief introduction to 11ACM 朱旻申. Outline What is Groovy ? Differences between Groovy & Java About closure & Dagger Methods.
Effective Java: Generics Last Updated: Spring 2009.
1 Computer Systems -- Introduction  Chapter 1 focuses on:  the structure of a Java application  basic program elements  preparing and executing a program.
INTRODUCTION. What is HTML? HTML is a language for describing web pages. HTML stands for Hyper Text Markup Language HTML is not a programming language,
Goals for Today  implement a Deck of Cards  composition  Iterator interface  Iterable interface 1.
Scala Overview Brandon Clopton CSCI 431. Scala fuses object-oriented and functional programming in a statically typed programming language. It is aimed.
JSTL, XML and XSLT An introduction to JSP Standard Tag Library and XML/XSLT transformation for Web layout.
XSLT Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
Copyright 2007, Information Builders. Slide 1 Understanding Basic HTML Amanda Regan Technical Director June, 2008.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson.
Freemarker ● Introduction ● Core features ● Java part example ● Template example ● Expressions ● Builtins ● Assigning value ● Conditions ● Loops ● Macros.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
E FFECTIVE C# 50 Specific Ways to Improve Your C# Second Edition Bill Wagner محمد حسین سلطانی.
XHTML By Trevor Adams. Topics Covered XHTML eXtensible HyperText Mark-up Language The beginning – HTML Web Standards Concept and syntax Elements (tags)
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
COP-3330: Object Oriented Programming Flow Control May 16, 2012 Eng. Hector M Lugo-Cordero, MS.
Sound Haskell Dana N. Xu University of Cambridge Joint work with Simon Peyton Jones Microsoft Research Cambridge Koen Claessen Chalmers University of Technology.
Functions and Methods. Definitions and types A function is a piece of code that takes arguments and returns a result A pure function is a function whose.
C# 2.0 and Future Directions Anders Hejlsberg Technical Fellow Microsoft Corporation.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
CompSci 100E 40.1 Java 5 New Features  Generics  Enhanced for loop  Autoboxing/unboxing  Typesafe enums  Other  Varargs  Static Import  Metadata.
Python Overview  Last week Python 3000 was released  Python 3000 == Python 3.0 == Py3k  Designed to break backwards compatibility with the 2.x.
Types in programming languages1 What are types, and why do we need them?
Java 5 Part 2 CSE301 University of Sunderland Harry Erwin, PhD.
CS162 Week 1 Kyle Dewey. Overview Basic Introduction CS Accounts Scala survival guide.
Representing data with XML SE-2030 Dr. Mark L. Hornick 1.
PROGRAMMING IN C#. Collection Classes (C# Programming Guide) The.NET Framework provides specialized classes for data storage and retrieval. These classes.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Getting Functional. Object-Oriented Programming in Scala Scala is object-oriented, and is based on Java’s model An object is a singleton object (there.
Session 02 Module 3: Statements and Operators Module 4: Programming constructs Module 5: Arrays.
CSE 341 : Programming Languages Lecture 9 Lexical Scope, Closures Zach Tatlock Spring 2014.
Lecture 9:FXML and Useful Java Collections Michael Hsu CSULA.
Functional Processing of Collections (Advanced) 6.0.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
Designing a DSL in KOTLIN
Functional Processing of Collections (Advanced)
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
.NET and .NET Core 9. Towards Higher Order Pan Wuming 2017.
COS 260 DAY 10 Tony Gauvin.
Iterator.
FP Foundations, Scheme In Text: Chapter 14.
Functional interface.
Chengyu Sun California State University, Los Angeles
Presentation transcript:

The Kotlin Programming Language Andrey Breslav Dmitry Jemerov

What is Kotlin? Statically typed JVM-targeted general-purpose programming language developed by JetBrains Docs available today Beta planned for the end of 2011

What is Kotlin? (II) Number of research papers we plan to publish related to Kotlin? Zero Or close to that…

Outline Motivation Basic constructs walk-through Higher-order functions Function literals Inline functions Type-safe Groovy-style builders Non-local returns Workshop Classes, Mixins and First-class delegation Generics: Variance annotations and Type projections Class objects Pattern matching

Motivation Why a new language? Design goals The existing ones do not fit with our needs And we had a close look at many of them Design goals Full Java interoperability Compiles as fast as Java Safer than Java More concise than Java Way simpler than Scala

Feature list Features: Higher-order functions Properties Mixins and First-class delegation Extension functions Static nullability checking Automatic casts Reified generics Declaration-site variance Modules and Build infrastructure (fighting the "Jar hell") Inline-functions (zero-overhead closures) Pattern matching … Full-featured IDE by JetBrains from the very beginning

Basic constructs walk-through IDE demo

Function types and literals Functions fun f(p : Int) : String { return p.toString() } Function types fun (p : Int) : String fun (Int) : String Function literals {(p : Int) : String => p.toString()} {(p : Int) => p.toString()} {p => p.toString()}

Higher-order functions fun filter<T>( c : Iterable<T>, f : fun (T) : Boolean ) : Iterable<T> val list = list("a", "ab", "abc", "") filter(list, {s => s.length() < 3}) yields ["a", "ab", ""] Convention: last function literal argument filter(list) {s => s.length() < 3} Convention: one-parameter function literal filter(list) {it.length() < 3}

Lock example (I) myLock.lock() try { // Do something } finally { myLock.unlock() }

Lock example (II) lock (myLock) { // Do something } fun lock( theLock : Lock, body : fun() : Unit )

Implementation General: works everywhere, but costs something Inner classes Method handles Special: may not work in some cases, but costs nothing Inline functions Kotlin features both general and special implementations

Lock example (III) inline fun lock( theLock : Lock, body : fun() : Unit ) { myLock.lock() try { body() } finally { myLock.unlock() }

Extension function literals Extension functions fun Int.f(p : Int) : String { return "…" } Extension function types fun Int.(p : Int) : String fun Int.(Int) : String Extension function literals {Int.(p : Int) : String => "…"} {Int.(p : Int) => "…"} {Int.(p) => "…"}

HTML example (I) Function definition Usage fun html(init : fun HTML.() : Unit) : HTML { val html = HTML() html.init() return html } Usage html { this.addMeta( httpEquiv="content-type", content="text/html;charset=UTF-8")

HTML example (II) Function definition Usage fun html(init : fun HTML.() : Unit) : HTML { val html = HTML() html.init() return html } Usage html { addMeta( httpEquiv="content-type", content="text/html;charset=UTF-8")

Builders in Groovy html { head { title "XML encoding with Groovy" } body { h1 "XML encoding with Groovy" p "this format can be used as an alternative markup to XML" /* an element with attributes and text content */ ahref:'http://groovy.codehaus.org' ["Groovy"]

Builders in Kotlin html { head { title { +"XML encoding with Kotlin" } } body { h1 { +"XML encoding with Kotlin" } p { +"this format can be used as an alternative markup to XML" } /* an element with attributes and text content */ a (href="http://jetbrains.com/kotlin") { +"Kotlin" } The big difference: the Kotlin version is statically type-checked

Builders in Kotlin: Implementation (I) 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() : TagWithText("html") { fun head(init : fun Head.() : Unit) { … } fun body(init : fun Body.() : Unit) { … } }

Builders in Kotlin: Implementation (II) fun html(init : fun HTML.() : Unit) : HTML { val html = HTML() html.init() return html } class HTML() : TagWithText("html") { fun head(init : fun Head.() : Unit) { val head = Head() head.init() children.add(head) } }

Builders in Kotlin: Implementation (III) a (href="http://jetbrains.com/kotlin") { +"Kotlin" } class BodyTag(name : String) : TagWithText(name) { fun a(href : String, init : fun A.() : Unit) : A { val a = A() a.init() a.attributes["href"] = href children.add(a) } }

Foreach example (I) inline fun <T> Iterable<T>.foreach( body : fun(T) : Unit ) { for (item in this) body(item) } Example usage: list map {it.length() > 2} foreach { print(it)

Foreach example (II) fun hasZero(list : List<Int>) : Boolean { // A call to an inline function list.foreach { if (it == 0) return true // Non-local return } return false Unqualified return always returns from a named function

Qualified returns @label {x => …} @label { x => } Function literals may be marked with labels: @label {x => …} To make a local return, use qualified form: @label { x => … return@label }

Labels, Break and Continue @outer for (x in list1) { for (y in list2) { if (…) { // Breaks the inner loop break } // Breaks the outer loop break@outer

Breaks in foreach() @outer list1.foreach { x => list2.foreach { y => if (…) { // Breaks the inner loop break } // Breaks the outer loop break@outer

Breakable foreach() inline fun <T> Iterable<T>.foreach( body : breakable fun(T) : Unit ) { @@ for (item in this) { // A break from body() breaks the loop body(item) }

Resources http://jetbrains.com/kotlin http://blog.jetbrains.com/kotlin @project_kotlin @intelliyole @abreslav

The Kotlin Programming Language Andrey Breslav Dmitry Jemerov