Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Kotlin and why you should love it #1
Search
Roberto Orgiu
June 23, 2017
Programming
0
83
Kotlin and why you should love it #1
Introductory slides about Kotlin
Roberto Orgiu
June 23, 2017
Tweet
Share
More Decks by Roberto Orgiu
See All by Roberto Orgiu
Wellness & Droid
tiwiz
0
92
Behind the curtains
tiwiz
0
37
The Importance of Being Tested
tiwiz
0
370
An Android Dev start to Kotlin MPP
tiwiz
0
130
Fantastic API and where to find them
tiwiz
0
51
Flipping the Koin @ GDG Dev Party
tiwiz
1
37
Flipping the Koin
tiwiz
2
140
Trip into the async world @ NYC Kotlin Meetup
tiwiz
0
81
Trip into the async world
tiwiz
1
100
Other Decks in Programming
See All in Programming
PHPによる"非"構造化プログラミング入門 -本当に熱いスパゲティコードを求めて- by きんじょうひでき
o0h
PRO
0
590
DevNexus - Create AI Infused Java Apps with LangChain4j
kdubois
0
170
研究開発と実装OSSと プロダクトの好循環 / A virtuous cycle of research and development implementation OSS and products
linyows
1
160
Devin入門 〜月500ドルから始まるAIチームメイトとの開発生活〜 / Introduction Devin 〜Development With AI Teammates〜
rkaga
6
2.2k
爆速スッキリ! Rspack 移行の成果と道のり - Muddy Web #11
dora1998
0
110
Windows版PHPのビルド手順とPHP 8.4における変更点
matsuo_atsushi
0
300
Devin , 正しい付き合い方と使い方 / Living and Working with Devin
yukinagae
1
430
Boost Your Web Performance with Hyperdrive
chimame
1
220
英語文法から学ぶ、クリーンな設計の秘訣
newnomad
1
230
はじめてのIssueOps - GitHub Actionsで実現するコメント駆動オペレーション
tmknom
7
2.1k
ローコードサービスの進化のためのモノレポ移行
taro28
1
240
私の愛したLaravel 〜レールを超えたその先へ〜
kentaroutakeda
2
580
Featured
See All Featured
Practical Orchestrator
shlominoach
186
10k
Adopting Sorbet at Scale
ufuk
75
9.3k
A Modern Web Designer's Workflow
chriscoyier
693
190k
We Have a Design System, Now What?
morganepeng
51
7.5k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
32
2.2k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
227
22k
Building Your Own Lightsaber
phodgson
104
6.3k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
7.1k
Docker and Python
trallard
44
3.3k
Art, The Web, and Tiny UX
lynnandtonic
298
20k
A designer walks into a library…
pauljervisheath
205
24k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
50
2.3k
Transcript
KOTLIN AND WHY YOU SHOULD LOVE IT
WHAT IS KOTLIN? > Compatible with JVM (and moar) >
Made by Jetbrains > More expressive > Safer > Functional > Uses Extension Functions > Highly interoperable
NULLABLE VS NONNULL > They are different types > Compile
time error if you assign null to a non null variable > No more NullPointerException !
NULLABLE VS NONNULL > val name: String = null ❌
> val name: String = "Roberto" ✔ > val name: String? = null ✔
NULLABLE VS NONNULL (INTEROPERABILITY) > Calling Java from Kotlin ➡
Nullable > Unless it's annotated with @NonNull / @NotNull
SMART CAST void function(Object something) { if(something instanceof String) {
String s = (String) something; println(s.substring(0, 3)) } }
SMART CAST fun function(something: Any) { if(something is String) {
println(something.substring(0, 3)) } }
if you need explicit casting... pluto as Dog
DECLARING A CLASS class Person { ... }
DECLARING A CLASS class Person (name : String, surname :
String?)
DECLARING A CLASS class Person (name : String, surname :
String?) { init { //code common to every constructor } }
DECLARING A CLASS class Person (name : String, surname :
String?) { constructor(name: String){ this(name, null) } init { //code common to every constructor } }
DECLARING A CLASS class Person (name : String, surname :
String? = null) { init { //code common to every constructor } }
CREATING AN INSTANCE > val p = Person("Pippo") > val
p2 = Person("Pippo", "Goofy")
CALLING A METHOD ON A NULLABLE TYPE val p =
Person("Pippo") val fifthCharOfSurname: Char? = p.surname?.charAt(4) //this is nullable type fifthCharOfSurname?.doSomething() //executed only if not null
NULLABLE TYPES IN JAVA Person p = new Person("Pippo", null);
String surname = p.getSurname(); if (surname != null) { Char fifthCharOfSurname = surname.charAt(4); if (fifthChar != null) { fifthCharOfSurname.doSomething(); } }
INHERITANCE class Person (name : String, surname : String) :
Animal (name) { ... }
INTERFACES interface OnClickListener { fun onClick(v : View) }
INTERFACES AND DEFAULT METHODS interface OnClickListener { fun onClick(v :
View) = println("I've been clicked") }
FUNCTIONS fun onCreate(savedInstanceState: Bundle?) { ... }
FUNCTIONS fun add(x : Int, y : Int) : Int
{ return x + y }
FUNCTIONS fun add(x : Int, y : Int) : Int
= x + y
FUNCTIONS fun add(x : Int, y : Int) = x
+ y
FUNCTIONS fun add(x : Int = 1, y : Int
= 2) = x + y
STRING TEMPLATES val s1 = "Hello, $who" val s2 =
"Hello, ${person.name}"
VARIABLES > No implicit conversion: everything must be converted >
Chars are not Ints, but we can convert them > Bitwise: | is or, & is and > Type can be inferred > Strings can be accessed as arrays
VARIABLES IMMUTABLE VS MUTABLE
VARIABLES val VS var
VARIABLES val a = 42 a = 43 // No
can do! var b = "hello" b = "ciao" // Yeah can do var c : Context = this
VARIABLES class Person { var name : String = ""
get() = field.toUppercase() set(value) { field = "Name: $value" } }
CONTROL FLOW
everything returns something
if val max = if (a > b) a else
b
fun resultBasedOnCondition(condition: Boolean) { return if (condition) "A" else "B"
}
when val type = when(fido) { is Dog -> "Dog"
is Cat -> "Cat" else -> error("I only know about dogs and cats") }
DATA CLASSES data class Person (val name : String, val
lastName : String)
DATA CLASSES GIVE US > equals() > hashCode() > copy()
EXTENSION METHODS //file Extension.kt fun Dog.bark() { ... } pluto.bark()
fido.bark()
EXTENSION METHODS and how they are converted in Java static
void bark(Dog dog) { ... } ExtensionKt.bark(pluto) ExtensionKt.bark(fido)
EXTENSION METHODS CAN BE DECLARED AS LOCAL FUNCTIONS
LAMBDAS
LAMBDAS ANONYMOUS FUNCTIONS FOR EVERYONE
JAVA view.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) {
doSomething(); } });
KOTLIN view.setOnClickListener(object : OnClickListener() { override fun onClick(v : View)
{ doSomething() } })
KOTLIN WITH LAMBDAS view.setOnClickListener({ view -> doSomething() })
KOTLIN WITH LAMBDAS view.setOnClickListener({ doSomething() })
KOTLIN WITH LAMBDAS view.setOnClickListener() { doSomething() }
KOTLIN WITH LAMBDAS view.setOnClickListener { doSomething() }
INLINE FUNCTIONS
INLINE FUNCTIONS they will be substituted by their code during
compilation, instead of doing the real call to a function. inline fun <T> with(t: T, body: T.() -> Unit) { t.body() }
HOW TO MAKE AN INLINE FUNCTION inline fun ifIsLollipop(code :
() -> Unit) { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { code() } }
THEN ifIsLollipop { window.setStatusBarColor(Color.BLACK) }
THANKS!