SlideShare a Scribd company logo
1 of 23
Bulletproofing your foot for
Kotlin
Enrique Zamudio
@chochosmx
javaMexico.org
#Hashtag
Who?
• Programming for a living since 1994
• Doing Java since 2000
• Experience in Java, C, C#, Objective-C,
Groovy, Scala
• Worked on Ceylon compiler 2012-2017
• Became Java Champion in 2015
#Hashtag
@chochosmx
Kotlin
• Created by JetBrains
• Static typing
• Not Java!
• Official Android support
• JVM, Javascript
#Hashtag
@twitter
Nice features
• Data classes
• Destructuring
• Lambdas, higher order functions, function refs, method
refs
• Top-level methods
• Declaration-site variance
• Type inference
• "Typesafe null"
• String interpolation
#Hashtag
@twitter
THE GOOD PARTS: DATA CLASSES
data class Person(val name:String,
val birthDate:Date)
•toString() and equals() automagically added
•no hashCode() though
THE GOOD PARTS: DESTRUCTURING
val p = Person("John Doe", today)
val (n, f) = p
val map = mapOf(1 to "one",
2 to "two")
for ((k, v) in map) {
...
}
THE GOOD PARTS: FUNCTIONS
fun foo() { ... }
fun bar(f:() -> (Unit)) = f()
bar(::foo)
bar( { println("baz") } )
THE GOOD PARTS: STRING INTERPOLATION
val p = Person("John Doe", today)
val (n, f) = p
println("Name: $n birth: ${p.birthDate}")
THE GOOD PARTS: TYPE INFERENCE
val p = "hello"
fun foo(bar:String) = bar.length
fun foo(bar:String) {
return bar.length
}
THE GOOD PARTS: SMART CASTS
val p:Any = "string"
if (p is String) {
println(p.toUpperCase())
}
NOT SO GOOD: STILL HAVE CASTS
val p:Any = 1
if (p is Int) {
println((p as String).toUpperCase())
}
THE GOOD PARTS: TYPESAFE NULL
var p = "hello"
p = null
var p:String? = "hello"
p = null
🚫
THE GOOD PARTS: TYPESAFE NULL
var x:String? = "hello"
fun foo(s:String) {...}
foo(x) //COMPILER ERROR
if (x != null) {
foo(x)
}
NOT GOOD: TYPESAFE NULL KILLSWITCH
var x:String? = "hello"
fun foo(s:String) {...}
foo(x!!) //RUNTIME ERROR
NOT GOOD: TYPESAFE NULL KILLSWITCH
fun foo(s:String?) {
println(s?.length)
}
foo(x?:"")
fun bar(s:String?) {
println(s!!.length)
}
foo(x!!)
✔
✘
GOOD: OPERATOR OVERLOADING
class Foo(val x:Int) {
operator fun plus(o:Foo) =
Foo(o.x + x)
operator fun times(o:Foo) =
Foo(o.x * x)
}
GOOD: OPERATOR OVERLOADING
val a = Foo(1)
val b = Foo(2)
println(a + b)
println(a * b)
println(a.plus(b))
println(a.times(b))
NOT SO GOOD: INFORMAL OPERATOR OVERLOADING
➤ No interfaces to define behavior
➤ Could have used static typing here
class Foo(val x:Int) {
operator fun plus(o:Foo) =
Foo(x+o.x)
fun plus(i:Int) = x+i
}
println(Foo(1)+1)
GOOD: EXTENSION METHODS
data class Person(val name:String,
val birthDate:Date)
fun Person.debug() {
println("I'm $name born on $birthDate")
}
NOT SO GOOD: EXTENSION OPERATORS
data class Person(val name:String,
val birthDate:Date)
operator fun Person.plus(other:Person) {
????
}
REALLY NOT GOOD: NONLOCAL RETURN
fun foo() {
listOf(1, 2, 3).forEach {
println(it)
if (it > 0) {
return
}
}
println("All done!")
}
Questions?
#Hashtag
#Hashtag
@chochosmx
github.com/chochos
enrique@ceylon-lang.org

More Related Content

Similar to Bulletproofing your foot for Kotlin

Hands on: The sexy side of JavaScript (feat. node.js)
Hands on: The sexy side of JavaScript (feat. node.js)Hands on: The sexy side of JavaScript (feat. node.js)
Hands on: The sexy side of JavaScript (feat. node.js)Francesco Pira
 
Go for Object Oriented Programmers or Object Oriented Programming without Obj...
Go for Object Oriented Programmers or Object Oriented Programming without Obj...Go for Object Oriented Programmers or Object Oriented Programming without Obj...
Go for Object Oriented Programmers or Object Oriented Programming without Obj...Steven Francia
 
Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2Kirill Rozov
 
Introduction to Scala for JCConf Taiwan
Introduction to Scala for JCConf TaiwanIntroduction to Scala for JCConf Taiwan
Introduction to Scala for JCConf TaiwanJimin Hsieh
 
Taking Kotlin to production, Seriously
Taking Kotlin to production, SeriouslyTaking Kotlin to production, Seriously
Taking Kotlin to production, SeriouslyHaim Yadid
 
Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are...
Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are...Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are...
Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are...Heroku
 
Joshua Wehner - Tomorrows Programming Languages Today
Joshua Wehner - Tomorrows Programming Languages TodayJoshua Wehner - Tomorrows Programming Languages Today
Joshua Wehner - Tomorrows Programming Languages TodayRefresh Events
 
Leveraging the Power of Graph Databases in PHP
Leveraging the Power of Graph Databases in PHPLeveraging the Power of Graph Databases in PHP
Leveraging the Power of Graph Databases in PHPJeremy Kendall
 
Exploring Koltin on Android
Exploring Koltin on AndroidExploring Koltin on Android
Exploring Koltin on AndroidDeepanshu Madan
 
Intermediate Swift Language by Apple
Intermediate Swift Language by AppleIntermediate Swift Language by Apple
Intermediate Swift Language by Applejamesfeng2
 
sbt, history of JSON libraries, microservices, and schema evolution (Tokyo ver)
sbt, history of JSON libraries, microservices, and schema evolution (Tokyo ver)sbt, history of JSON libraries, microservices, and schema evolution (Tokyo ver)
sbt, history of JSON libraries, microservices, and schema evolution (Tokyo ver)Eugene Yokota
 
Making JavaScript Libraries More Approachable
Making JavaScript Libraries More ApproachableMaking JavaScript Libraries More Approachable
Making JavaScript Libraries More ApproachablePamela Fox
 
Por qué Crystal? Why Crystal Language?
Por qué Crystal? Why Crystal Language?Por qué Crystal? Why Crystal Language?
Por qué Crystal? Why Crystal Language?Crystal Language
 
Scala in practice - 3 years later
Scala in practice - 3 years laterScala in practice - 3 years later
Scala in practice - 3 years laterpatforna
 
Scala in-practice-3-years by Patric Fornasier, Springr, presented at Pune Sca...
Scala in-practice-3-years by Patric Fornasier, Springr, presented at Pune Sca...Scala in-practice-3-years by Patric Fornasier, Springr, presented at Pune Sca...
Scala in-practice-3-years by Patric Fornasier, Springr, presented at Pune Sca...Thoughtworks
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to PerlSway Wang
 
C# 6 and 7 and Futures 20180607
C# 6 and 7 and Futures 20180607C# 6 and 7 and Futures 20180607
C# 6 and 7 and Futures 20180607Kevin Hazzard
 
Why Java Sucks and C# Rocks (Final)
Why Java Sucks and C# Rocks (Final)Why Java Sucks and C# Rocks (Final)
Why Java Sucks and C# Rocks (Final)jeffz
 
Elixir - GDG - Nantes
Elixir - GDG - NantesElixir - GDG - Nantes
Elixir - GDG - NantesAxel CATELAND
 

Similar to Bulletproofing your foot for Kotlin (20)

Hands on: The sexy side of JavaScript (feat. node.js)
Hands on: The sexy side of JavaScript (feat. node.js)Hands on: The sexy side of JavaScript (feat. node.js)
Hands on: The sexy side of JavaScript (feat. node.js)
 
Go for Object Oriented Programmers or Object Oriented Programming without Obj...
Go for Object Oriented Programmers or Object Oriented Programming without Obj...Go for Object Oriented Programmers or Object Oriented Programming without Obj...
Go for Object Oriented Programmers or Object Oriented Programming without Obj...
 
Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2Kotlin Basics - Apalon Kotlin Sprint Part 2
Kotlin Basics - Apalon Kotlin Sprint Part 2
 
Introduction to Scala for JCConf Taiwan
Introduction to Scala for JCConf TaiwanIntroduction to Scala for JCConf Taiwan
Introduction to Scala for JCConf Taiwan
 
Taking Kotlin to production, Seriously
Taking Kotlin to production, SeriouslyTaking Kotlin to production, Seriously
Taking Kotlin to production, Seriously
 
Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are...
Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are...Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are...
Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are...
 
Joshua Wehner - Tomorrows Programming Languages Today
Joshua Wehner - Tomorrows Programming Languages TodayJoshua Wehner - Tomorrows Programming Languages Today
Joshua Wehner - Tomorrows Programming Languages Today
 
Leveraging the Power of Graph Databases in PHP
Leveraging the Power of Graph Databases in PHPLeveraging the Power of Graph Databases in PHP
Leveraging the Power of Graph Databases in PHP
 
Exploring Koltin on Android
Exploring Koltin on AndroidExploring Koltin on Android
Exploring Koltin on Android
 
Intermediate Swift Language by Apple
Intermediate Swift Language by AppleIntermediate Swift Language by Apple
Intermediate Swift Language by Apple
 
Swift School #1
Swift School #1Swift School #1
Swift School #1
 
sbt, history of JSON libraries, microservices, and schema evolution (Tokyo ver)
sbt, history of JSON libraries, microservices, and schema evolution (Tokyo ver)sbt, history of JSON libraries, microservices, and schema evolution (Tokyo ver)
sbt, history of JSON libraries, microservices, and schema evolution (Tokyo ver)
 
Making JavaScript Libraries More Approachable
Making JavaScript Libraries More ApproachableMaking JavaScript Libraries More Approachable
Making JavaScript Libraries More Approachable
 
Por qué Crystal? Why Crystal Language?
Por qué Crystal? Why Crystal Language?Por qué Crystal? Why Crystal Language?
Por qué Crystal? Why Crystal Language?
 
Scala in practice - 3 years later
Scala in practice - 3 years laterScala in practice - 3 years later
Scala in practice - 3 years later
 
Scala in-practice-3-years by Patric Fornasier, Springr, presented at Pune Sca...
Scala in-practice-3-years by Patric Fornasier, Springr, presented at Pune Sca...Scala in-practice-3-years by Patric Fornasier, Springr, presented at Pune Sca...
Scala in-practice-3-years by Patric Fornasier, Springr, presented at Pune Sca...
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
C# 6 and 7 and Futures 20180607
C# 6 and 7 and Futures 20180607C# 6 and 7 and Futures 20180607
C# 6 and 7 and Futures 20180607
 
Why Java Sucks and C# Rocks (Final)
Why Java Sucks and C# Rocks (Final)Why Java Sucks and C# Rocks (Final)
Why Java Sucks and C# Rocks (Final)
 
Elixir - GDG - Nantes
Elixir - GDG - NantesElixir - GDG - Nantes
Elixir - GDG - Nantes
 

More from Enrique Zamudio López

More from Enrique Zamudio López (8)

Introducción a Protocol Buffers
Introducción a Protocol BuffersIntroducción a Protocol Buffers
Introducción a Protocol Buffers
 
Developing Android applications with Ceylon
Developing Android applications with Ceylon Developing Android applications with Ceylon
Developing Android applications with Ceylon
 
Sistemas de tipos: Lo bueno, lo malo y lo feo
Sistemas de tipos: Lo bueno, lo malo y lo feoSistemas de tipos: Lo bueno, lo malo y lo feo
Sistemas de tipos: Lo bueno, lo malo y lo feo
 
Criptografía para simples mortales
Criptografía para simples mortalesCriptografía para simples mortales
Criptografía para simples mortales
 
Diseño de compiladores: Un vistazo a Ceylon-JS
Diseño de compiladores: Un vistazo a Ceylon-JSDiseño de compiladores: Un vistazo a Ceylon-JS
Diseño de compiladores: Un vistazo a Ceylon-JS
 
Introducción a Ceylon
Introducción a CeylonIntroducción a Ceylon
Introducción a Ceylon
 
PCJ Sesión 9: Threads
PCJ Sesión 9: ThreadsPCJ Sesión 9: Threads
PCJ Sesión 9: Threads
 
Introducción a jAlarms
Introducción a jAlarmsIntroducción a jAlarms
Introducción a jAlarms
 

Recently uploaded

why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 

Recently uploaded (20)

Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 

Bulletproofing your foot for Kotlin

  • 1. Bulletproofing your foot for Kotlin Enrique Zamudio @chochosmx javaMexico.org #Hashtag
  • 2. Who? • Programming for a living since 1994 • Doing Java since 2000 • Experience in Java, C, C#, Objective-C, Groovy, Scala • Worked on Ceylon compiler 2012-2017 • Became Java Champion in 2015 #Hashtag @chochosmx
  • 3. Kotlin • Created by JetBrains • Static typing • Not Java! • Official Android support • JVM, Javascript #Hashtag @twitter
  • 4. Nice features • Data classes • Destructuring • Lambdas, higher order functions, function refs, method refs • Top-level methods • Declaration-site variance • Type inference • "Typesafe null" • String interpolation #Hashtag @twitter
  • 5. THE GOOD PARTS: DATA CLASSES data class Person(val name:String, val birthDate:Date) •toString() and equals() automagically added •no hashCode() though
  • 6. THE GOOD PARTS: DESTRUCTURING val p = Person("John Doe", today) val (n, f) = p val map = mapOf(1 to "one", 2 to "two") for ((k, v) in map) { ... }
  • 7. THE GOOD PARTS: FUNCTIONS fun foo() { ... } fun bar(f:() -> (Unit)) = f() bar(::foo) bar( { println("baz") } )
  • 8. THE GOOD PARTS: STRING INTERPOLATION val p = Person("John Doe", today) val (n, f) = p println("Name: $n birth: ${p.birthDate}")
  • 9. THE GOOD PARTS: TYPE INFERENCE val p = "hello" fun foo(bar:String) = bar.length fun foo(bar:String) { return bar.length }
  • 10. THE GOOD PARTS: SMART CASTS val p:Any = "string" if (p is String) { println(p.toUpperCase()) }
  • 11. NOT SO GOOD: STILL HAVE CASTS val p:Any = 1 if (p is Int) { println((p as String).toUpperCase()) }
  • 12. THE GOOD PARTS: TYPESAFE NULL var p = "hello" p = null var p:String? = "hello" p = null 🚫
  • 13. THE GOOD PARTS: TYPESAFE NULL var x:String? = "hello" fun foo(s:String) {...} foo(x) //COMPILER ERROR if (x != null) { foo(x) }
  • 14. NOT GOOD: TYPESAFE NULL KILLSWITCH var x:String? = "hello" fun foo(s:String) {...} foo(x!!) //RUNTIME ERROR
  • 15. NOT GOOD: TYPESAFE NULL KILLSWITCH fun foo(s:String?) { println(s?.length) } foo(x?:"") fun bar(s:String?) { println(s!!.length) } foo(x!!) ✔ ✘
  • 16. GOOD: OPERATOR OVERLOADING class Foo(val x:Int) { operator fun plus(o:Foo) = Foo(o.x + x) operator fun times(o:Foo) = Foo(o.x * x) }
  • 17. GOOD: OPERATOR OVERLOADING val a = Foo(1) val b = Foo(2) println(a + b) println(a * b) println(a.plus(b)) println(a.times(b))
  • 18. NOT SO GOOD: INFORMAL OPERATOR OVERLOADING ➤ No interfaces to define behavior ➤ Could have used static typing here class Foo(val x:Int) { operator fun plus(o:Foo) = Foo(x+o.x) fun plus(i:Int) = x+i } println(Foo(1)+1)
  • 19. GOOD: EXTENSION METHODS data class Person(val name:String, val birthDate:Date) fun Person.debug() { println("I'm $name born on $birthDate") }
  • 20. NOT SO GOOD: EXTENSION OPERATORS data class Person(val name:String, val birthDate:Date) operator fun Person.plus(other:Person) { ???? }
  • 21. REALLY NOT GOOD: NONLOCAL RETURN fun foo() { listOf(1, 2, 3).forEach { println(it) if (it > 0) { return } } println("All done!") }