39
Language Design TradeOffs andrey.breslav@ .com is

Language Design Tradeoffs - Kotlin and Beyond, by Andrey Breslav

Embed Size (px)

Citation preview

Page 1: Language Design Tradeoffs - Kotlin and Beyond, by Andrey Breslav

Language  Design    Trade-­‐Offs  

andrey.breslav@ .com

is  

Page 2: Language Design Tradeoffs - Kotlin and Beyond, by Andrey Breslav

Simply  Universal  

Page 3: Language Design Tradeoffs - Kotlin and Beyond, by Andrey Breslav

Full  of  Shortcuts  

Page 4: Language Design Tradeoffs - Kotlin and Beyond, by Andrey Breslav

Simply  Universal  

Page 5: Language Design Tradeoffs - Kotlin and Beyond, by Andrey Breslav

Full  of  Shortcuts  

Page 6: Language Design Tradeoffs - Kotlin and Beyond, by Andrey Breslav

Trade-­‐Off  #0  

Elegance   Convenience  

Page 7: Language Design Tradeoffs - Kotlin and Beyond, by Andrey Breslav

Time  

Complexity  

1968  

Page 8: Language Design Tradeoffs - Kotlin and Beyond, by Andrey Breslav

Cool   PracHcal  

Page 9: Language Design Tradeoffs - Kotlin and Beyond, by Andrey Breslav

Clever   Readable  

Simple   Fast  

Shiny  New   Good  Old  

Ground-­‐Breaking   CompaHble  

Page 10: Language Design Tradeoffs - Kotlin and Beyond, by Andrey Breslav

Shiny  New   Good  Old  

Page 11: Language Design Tradeoffs - Kotlin and Beyond, by Andrey Breslav

As  Java  Evolved…  

Foo<? extends T> !

new Foo() !

new Foo<T>() !

new Foo<>() !

SAM-Conversions !

Default !methods !

Page 12: Language Design Tradeoffs - Kotlin and Beyond, by Andrey Breslav

Lambdas  &  SAM-­‐Conversions  

void foo(Function<? super Bar, ? extends Baz> g) !

SAM-­‐type  

foo(g -> new SubBaz(g)); !

Page 13: Language Design Tradeoffs - Kotlin and Beyond, by Andrey Breslav

CompaHbility  is  a  drag,  son…  

Page 14: Language Design Tradeoffs - Kotlin and Beyond, by Andrey Breslav

Simple   Fast  

Page 15: Language Design Tradeoffs - Kotlin and Beyond, by Andrey Breslav

Boxing  for  Primitives  

new Foo() !

The  Age  of  (Auto)Boxing  

Page 16: Language Design Tradeoffs - Kotlin and Beyond, by Andrey Breslav

Language  

RunHme  

JIT  

x86   amd64   ARM  

GC  

x86   amd64   ARM  

Threads  

Linux   Windows   Mac  

Don’t  touch  it!  

 

Hard  

Very  Hard  

Page 17: Language Design Tradeoffs - Kotlin and Beyond, by Andrey Breslav

Ground-­‐Breaking   CompaHble  

Page 18: Language Design Tradeoffs - Kotlin and Beyond, by Andrey Breslav

JDK Collections interface List<E> { E get(int index); E set(int index, E value); }

Read-only /** read-only */ List<Foo> getFoos() { return unmodifiableList(l); }

Errors At Runtime

Page 19: Language Design Tradeoffs - Kotlin and Beyond, by Andrey Breslav

Cool Collections interface List<E> { E get(int index); } interface MutableList<E> extends List<E> { E set(int index, E value); }

Read-only List<Foo> getFoos() { return l; // may be mutable }

Errors At Compile Time

Page 20: Language Design Tradeoffs - Kotlin and Beyond, by Andrey Breslav

Safe   CompaHble  

Cool Collections JDK Collections

catch  errors  early  self-­‐documenHng  code  

no  wrapping/repackaging  exisHng  API’s  work  

IntuiHve   ?  extends  Foo  

print(List<? extends Foo> foos) print(foos: List<Foo>)

Page 21: Language Design Tradeoffs - Kotlin and Beyond, by Andrey Breslav

Iterable<out  T>  

CollecHon<out  T>  

List<out  T>   Set<out  T>  

MutableIterable<T>  

MutableCollecHon<T>  

MutableList<T>   MutableSet<T>  

java.lang.ArrayList<T>   java.uHl.HashSet<T>  

Kotlin Collections

JDK Collections

Page 22: Language Design Tradeoffs - Kotlin and Beyond, by Andrey Breslav

Rare  luck  

Kotlin  CollecHons  =                                &  

Safe  

CompaHble  

Co-­‐variant  

Page 23: Language Design Tradeoffs - Kotlin and Beyond, by Andrey Breslav

Java String s = null; s.length();

Errors At Runtime

Kotlin val s: String s.length() val s: String? = null s.length()

Errors At Compile Time

= null

Nullable  type  

Page 24: Language Design Tradeoffs - Kotlin and Beyond, by Andrey Breslav

Check and use val s: String? = … if (s != null) { s.length() }

Check and exit if (s == null) return s.length()

Rock’n’Roll s?.length()

Page 25: Language Design Tradeoffs - Kotlin and Beyond, by Andrey Breslav

JAR  File  

class Foo { String getName() { … } }

Nullable  or  Not?  

A  hell  of  a  smart  

tool  

<XML/>  

@NotNull  

KAnnotator  

Page 26: Language Design Tradeoffs - Kotlin and Beyond, by Andrey Breslav

Dynamic   StaHc  

Page 27: Language Design Tradeoffs - Kotlin and Beyond, by Andrey Breslav

Library  

Locked  

*UHl  class  

Page 28: Language Design Tradeoffs - Kotlin and Beyond, by Andrey Breslav

Util class Collections2.transform( Collections2.filter( list, (foo) -> foo.isValid()), (foo) -> new Bar(foo)));

No Util Class scala.collection.Iterable[T]

declares 116 methods

Page 29: Language Design Tradeoffs - Kotlin and Beyond, by Andrey Breslav

Ugly  Interfaces   Ugly  Code  

Page 30: Language Design Tradeoffs - Kotlin and Beyond, by Andrey Breslav
Page 31: Language Design Tradeoffs - Kotlin and Beyond, by Andrey Breslav

Java static char last(String s)

Usage “abc”.last()

C# static char last(string this)

Kotlin fun String.last(): Char { return this[this.size - 1] }

Page 32: Language Design Tradeoffs - Kotlin and Beyond, by Andrey Breslav

Access privates No

Virtual No

Owner Package

Many Applicable Overloading error

Member Clashes Member wins

=  staHc  uHlity  

Page 33: Language Design Tradeoffs - Kotlin and Beyond, by Andrey Breslav
Page 34: Language Design Tradeoffs - Kotlin and Beyond, by Andrey Breslav

Theory   PracHce  

Page 35: Language Design Tradeoffs - Kotlin and Beyond, by Andrey Breslav

Java new List<String>() Collections.<String>emptyList()

Why different?

Scala new List[String]() emptyList[String]() arr(0)

Why square brackets?

Page 36: Language Design Tradeoffs - Kotlin and Beyond, by Andrey Breslav

foo(bar<A, B>(x + 1))

foo(bar < a, b > (x + 1))

Page 37: Language Design Tradeoffs - Kotlin and Beyond, by Andrey Breslav

Java Familiar (from C++) Sometimes ugly

Scala Always pretty Unfamiliar

C# and Kotlin

Familiar Always* pretty

emptyList<String>()

* foo((bar < A), B > (x+1))

Page 38: Language Design Tradeoffs - Kotlin and Beyond, by Andrey Breslav

PracHce    is  nicer  than    

Theory  This  Hme  

Page 39: Language Design Tradeoffs - Kotlin and Beyond, by Andrey Breslav

Language  Design    Trade-­‐Offs  

andrey.breslav@ .com

is  

kotlin.jetbrains.org