Language Design Tradeoffs (Kotlin and Beyond) by Andrey Breslav
-
Upload
zeroturnaround -
Category
Technology
-
view
375 -
download
0
Transcript of Language Design Tradeoffs (Kotlin and Beyond) by Andrey Breslav
![Page 1: Language Design Tradeoffs (Kotlin and Beyond) by Andrey Breslav](https://reader033.fdocuments.us/reader033/viewer/2022060108/554f3d8cb4c905cd048b521b/html5/thumbnails/1.jpg)
Language Design Trade-‐Offs
andrey.breslav@ .com
is
![Page 2: Language Design Tradeoffs (Kotlin and Beyond) by Andrey Breslav](https://reader033.fdocuments.us/reader033/viewer/2022060108/554f3d8cb4c905cd048b521b/html5/thumbnails/2.jpg)
Simply Universal
![Page 3: Language Design Tradeoffs (Kotlin and Beyond) by Andrey Breslav](https://reader033.fdocuments.us/reader033/viewer/2022060108/554f3d8cb4c905cd048b521b/html5/thumbnails/3.jpg)
Full of Shortcuts
![Page 4: Language Design Tradeoffs (Kotlin and Beyond) by Andrey Breslav](https://reader033.fdocuments.us/reader033/viewer/2022060108/554f3d8cb4c905cd048b521b/html5/thumbnails/4.jpg)
Simply Universal
![Page 5: Language Design Tradeoffs (Kotlin and Beyond) by Andrey Breslav](https://reader033.fdocuments.us/reader033/viewer/2022060108/554f3d8cb4c905cd048b521b/html5/thumbnails/5.jpg)
Full of Shortcuts
![Page 6: Language Design Tradeoffs (Kotlin and Beyond) by Andrey Breslav](https://reader033.fdocuments.us/reader033/viewer/2022060108/554f3d8cb4c905cd048b521b/html5/thumbnails/6.jpg)
Trade-‐Off #0
Elegance Convenience
![Page 7: Language Design Tradeoffs (Kotlin and Beyond) by Andrey Breslav](https://reader033.fdocuments.us/reader033/viewer/2022060108/554f3d8cb4c905cd048b521b/html5/thumbnails/7.jpg)
Time
Complexity
1968
![Page 8: Language Design Tradeoffs (Kotlin and Beyond) by Andrey Breslav](https://reader033.fdocuments.us/reader033/viewer/2022060108/554f3d8cb4c905cd048b521b/html5/thumbnails/8.jpg)
Cool PracHcal
![Page 9: Language Design Tradeoffs (Kotlin and Beyond) by Andrey Breslav](https://reader033.fdocuments.us/reader033/viewer/2022060108/554f3d8cb4c905cd048b521b/html5/thumbnails/9.jpg)
Clever Readable
Simple Fast
Shiny New Good Old
Ground-‐Breaking CompaHble
![Page 10: Language Design Tradeoffs (Kotlin and Beyond) by Andrey Breslav](https://reader033.fdocuments.us/reader033/viewer/2022060108/554f3d8cb4c905cd048b521b/html5/thumbnails/10.jpg)
Shiny New Good Old
![Page 11: Language Design Tradeoffs (Kotlin and Beyond) by Andrey Breslav](https://reader033.fdocuments.us/reader033/viewer/2022060108/554f3d8cb4c905cd048b521b/html5/thumbnails/11.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022060108/554f3d8cb4c905cd048b521b/html5/thumbnails/12.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022060108/554f3d8cb4c905cd048b521b/html5/thumbnails/13.jpg)
CompaHbility is a drag, son…
![Page 14: Language Design Tradeoffs (Kotlin and Beyond) by Andrey Breslav](https://reader033.fdocuments.us/reader033/viewer/2022060108/554f3d8cb4c905cd048b521b/html5/thumbnails/14.jpg)
Simple Fast
![Page 15: Language Design Tradeoffs (Kotlin and Beyond) by Andrey Breslav](https://reader033.fdocuments.us/reader033/viewer/2022060108/554f3d8cb4c905cd048b521b/html5/thumbnails/15.jpg)
Boxing for Primitives
new Foo() !
The Age of (Auto)Boxing
![Page 16: Language Design Tradeoffs (Kotlin and Beyond) by Andrey Breslav](https://reader033.fdocuments.us/reader033/viewer/2022060108/554f3d8cb4c905cd048b521b/html5/thumbnails/16.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022060108/554f3d8cb4c905cd048b521b/html5/thumbnails/17.jpg)
Ground-‐Breaking CompaHble
![Page 18: Language Design Tradeoffs (Kotlin and Beyond) by Andrey Breslav](https://reader033.fdocuments.us/reader033/viewer/2022060108/554f3d8cb4c905cd048b521b/html5/thumbnails/18.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022060108/554f3d8cb4c905cd048b521b/html5/thumbnails/19.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022060108/554f3d8cb4c905cd048b521b/html5/thumbnails/20.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022060108/554f3d8cb4c905cd048b521b/html5/thumbnails/21.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022060108/554f3d8cb4c905cd048b521b/html5/thumbnails/22.jpg)
Rare luck
Kotlin CollecHons = &
Safe
CompaHble
Co-‐variant
![Page 23: Language Design Tradeoffs (Kotlin and Beyond) by Andrey Breslav](https://reader033.fdocuments.us/reader033/viewer/2022060108/554f3d8cb4c905cd048b521b/html5/thumbnails/23.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022060108/554f3d8cb4c905cd048b521b/html5/thumbnails/24.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022060108/554f3d8cb4c905cd048b521b/html5/thumbnails/25.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022060108/554f3d8cb4c905cd048b521b/html5/thumbnails/26.jpg)
Dynamic StaHc
![Page 27: Language Design Tradeoffs (Kotlin and Beyond) by Andrey Breslav](https://reader033.fdocuments.us/reader033/viewer/2022060108/554f3d8cb4c905cd048b521b/html5/thumbnails/27.jpg)
Library
Locked
*UHl class
![Page 28: Language Design Tradeoffs (Kotlin and Beyond) by Andrey Breslav](https://reader033.fdocuments.us/reader033/viewer/2022060108/554f3d8cb4c905cd048b521b/html5/thumbnails/28.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022060108/554f3d8cb4c905cd048b521b/html5/thumbnails/29.jpg)
Ugly Interfaces Ugly Code
![Page 30: Language Design Tradeoffs (Kotlin and Beyond) by Andrey Breslav](https://reader033.fdocuments.us/reader033/viewer/2022060108/554f3d8cb4c905cd048b521b/html5/thumbnails/30.jpg)
![Page 31: Language Design Tradeoffs (Kotlin and Beyond) by Andrey Breslav](https://reader033.fdocuments.us/reader033/viewer/2022060108/554f3d8cb4c905cd048b521b/html5/thumbnails/31.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022060108/554f3d8cb4c905cd048b521b/html5/thumbnails/32.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022060108/554f3d8cb4c905cd048b521b/html5/thumbnails/33.jpg)
![Page 34: Language Design Tradeoffs (Kotlin and Beyond) by Andrey Breslav](https://reader033.fdocuments.us/reader033/viewer/2022060108/554f3d8cb4c905cd048b521b/html5/thumbnails/34.jpg)
Theory PracHce
![Page 35: Language Design Tradeoffs (Kotlin and Beyond) by Andrey Breslav](https://reader033.fdocuments.us/reader033/viewer/2022060108/554f3d8cb4c905cd048b521b/html5/thumbnails/35.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022060108/554f3d8cb4c905cd048b521b/html5/thumbnails/36.jpg)
foo(bar<A, B>(x + 1))
foo(bar < a, b > (x + 1))
![Page 37: Language Design Tradeoffs (Kotlin and Beyond) by Andrey Breslav](https://reader033.fdocuments.us/reader033/viewer/2022060108/554f3d8cb4c905cd048b521b/html5/thumbnails/37.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022060108/554f3d8cb4c905cd048b521b/html5/thumbnails/38.jpg)
PracHce is nicer than
Theory This Hme
![Page 39: Language Design Tradeoffs (Kotlin and Beyond) by Andrey Breslav](https://reader033.fdocuments.us/reader033/viewer/2022060108/554f3d8cb4c905cd048b521b/html5/thumbnails/39.jpg)
Language Design Trade-‐Offs
andrey.breslav@ .com
is
kotlin.jetbrains.org