SlideShare a Scribd company logo
Mixing
Scala and Kotlin
March 2021
Alexey Soshin
Solutions Architect
Agenda
● What is Depop
● Why?!
● Collections
● Nullability
● Functions
● Coroutines
Depop
Building the most diverse, progressive
Home of Fashion.
● Fashion marketplace app for the next generation
to buy, sell and discover unique fashion.
● Community of like minded creatives, young
entrepreneurs and sustainable enthusiasts who
are transforming the fashion industry.
● Globally, over 26 million users on the platform in
147 countries, and 90% of our active users are
under the age of 26, or Gen Z.
● As an app based platform, Depop combines the
familiarity of social media with the mechanics of
a resale marketplace.
● Our Mission is to Empower our community to
create a new sustainable and equitable fashion
system.
Motivation
Scala was there first
● Scala first appeared around 2004
● Kotlin - 2011, out of Beta in 2016
The Dinosaur in the Room
Scala engineers are rare
Scala is F[_] hard
Mobile Developers ❤️ Kotlin
● Kotlin is the main language
on Android
● Conceptually closer to Swift
than Scala
● Great for Backend For
Frontend approach, where
Mobile engineers control the
API layer
Map[String, String] or
Map<String, String>?
def doSomething(properties: Map[String, List[String]])
fun doSomething(properties: Map<String, List<String>>)
Type mismatch:
inferred type is kotlin.collections.Map<String, kotlin.collections.List<String>>
but scala.collection.immutable.Map<String!,
scala.collection.immutable.List<String!>!>! was expected
🤔
😱
Collections
Collections
def doSomething(m: Map[String, List[String]])
doSomething(mapOf("hello" to listOf("world")))
import scala.collection.JavaConverters._
def doSomething(m: java.util.Map[String,
java.util.List[String]])
Collections
def doSomething(m: java.util.Map[String,
java.util.List[String]]) = {
val a: mutable.Map[String, util.List[String]] =
m.asScala
val b: collection.Map[String, List[String]] =
a.mapValues(l => l.asScala.toList)
doSomething(b.toMap)
}
def doSomething(m: Map[String, List[String]])
Scala Option vs Kotlin
nullability
def doSomething(value: Option[String] = None)
fun doSomething(value: String?)
🤔
Nullability
Type mismatch:
Required: Option<String!>!
Found: String?
inline fun <reified T> none() = Option.empty<T>()
inline fun <reified T> some(t: T) = Option.apply(t)
if (value == null) none() else some(value)
private fun Any?.asOption() = if (this == null) none() else some(this)
Nullability
When a Function is not
a Function
def executeBlock(block: () => Unit) = {
println("I'm going to execute it!")
block()
}
fun executeBlock(block: () -> Unit) {
println("I'm going to execute it!")
block()
}
Functions
=?
MainKt.executeBlock(() => println("Hello from Scala"))
Error: type mismatch;
found : Unit (in scala)
required: Unit (in kotlin)
MainKt.executeBlock(() => println("hello"))
Functions
def executeBlock(block: () => Unit) = {
println("I'm going to execute it!")
block()
}
≠
fun executeBlock(block: () -> Unit) {
println("I'm going to execute it!")
block()
}
kotlin.jvm.functions.Function0 scala.Function0
Functions
def executeBlock(block: () => Int): Int = {
println("I'm going to execute it!")
block()
1
}
=?
fun executeBlock(block: () -> Int): Int {
println("I'm going to execute it!")
block()
return 1
}
kotlin.jvm.functions.Function0 scala.Function0
≠
Functions
MainKt.executeBlock(() => 1)
ScalaObject.executeBlock {
println("Hello from Kotlin")
1
}
👍
👍
Functions
Coroutines
class AsyncKotlinClass : CoroutineScope {
override val coroutineContext = Dispatchers.Default
fun getCatAsync(name: String) = async {
Cat(name)
}
suspend fun getCat(name: String): Cat {
delay(100)
return Cat(name)
}
}
Coroutines
val asyncKotlinClass = new AsyncKotlinClass()
val res = asyncKotlinClass.getCatAsync("Fluffy")
val cat = res.await()
Error: not enough arguments for method await:
(x$1: kotlin.coroutines.Continuation[_ >: Cat])Object.
Unspecified value parameter x$1.
val cat = res.await()
Coroutines
val asyncKotlinClass = new AsyncKotlinClass()
val cat = asyncKotlinClass.getCat("Fluffy")
Error: not enough arguments for method getCat:
(x$1: String, x$2: kotlin.coroutines.Continuation[_ >: Cat])Object.
Unspecified value parameter x$2.
val cat = asyncKotlinClass.getCat("Fluffy")
😱
Coroutines
Coroutines
import kotlinx.coroutines.future.*
fun getCatAsync(name: String) =
async {
Cat(name)
}.asCompletableFuture()
import
scala.compat.java8.FutureConverters._
val asyncKotlinClass = new
AsyncKotlinClass()
val res =
asyncKotlinClass.getCatAsync("Fluffy")
val cat = toScala(res)
Coroutines
Summary
● Reasons to adopt Kotlin in a Scala company:
○ Easier to adopt
○ Mobile Developers love it
● Reasons to keep using Scala
○ Very powerful language
○ Lots of code already written in it
● If you need to mix Scala and Kotlin
○ Easier to call Kotlin from Scala than Scala from
Kotlin
We’re Hiring!
Our Blog: https://engineering.depop.com
Tech careers:
https://boards.greenhouse.io/depop
Stay connected:
https://alexey-soshin.medium.com/
https://twitter.com/alexey_soshin
https://www.udemy.com/user/alexey-soshin/
THANK YOU

More Related Content

What's hot

The Types of Cryptocurrency Wallets
The Types of Cryptocurrency WalletsThe Types of Cryptocurrency Wallets
The Types of Cryptocurrency Wallets
Ducatus Global
 
chatgpt dalle.pptx
chatgpt dalle.pptxchatgpt dalle.pptx
chatgpt dalle.pptx
Ellen Edmands
 
From gaming to the metaverse
From gaming to the metaverseFrom gaming to the metaverse
From gaming to the metaverse
Alison B. Lowndes
 
プロダクトのフェーズとマネジメント
プロダクトのフェーズとマネジメントプロダクトのフェーズとマネジメント
プロダクトのフェーズとマネジメント
Takamasa Hirose
 
Specification-By-Example with Gherkin
Specification-By-Example with GherkinSpecification-By-Example with Gherkin
Specification-By-Example with Gherkin
Christian Hassa
 
ユーザーエクスペリエンスの分解
ユーザーエクスペリエンスの分解ユーザーエクスペリエンスの分解
ユーザーエクスペリエンスの分解Takehisa Gokaichi
 
Kids coding
Kids codingKids coding
Kids coding
Caryn Farvour
 
Is Metaverse The Next Big Thing?
Is Metaverse The Next Big Thing?Is Metaverse The Next Big Thing?
Is Metaverse The Next Big Thing?
101 Blockchains
 
Web3 Market Overview Global & Japan
Web3 Market Overview Global & JapanWeb3 Market Overview Global & Japan
Web3 Market Overview Global & Japan
Taiki Narita
 
お客様の目を覚ませ! ついでに自分の目も覚ませ! デザイン思考のクライアントワークのプレセールス
お客様の目を覚ませ! ついでに自分の目も覚ませ! デザイン思考のクライアントワークのプレセールスお客様の目を覚ませ! ついでに自分の目も覚ませ! デザイン思考のクライアントワークのプレセールス
お客様の目を覚ませ! ついでに自分の目も覚ませ! デザイン思考のクライアントワークのプレセールス
Yoshiki Hayama
 
Crypto 101
Crypto 101Crypto 101
Crypto 101
Marie Tatibouet
 
3 Business Problems Chatbots Solve Best
3 Business Problems Chatbots Solve Best3 Business Problems Chatbots Solve Best
3 Business Problems Chatbots Solve Best
Haptik
 
How NFT Works
How NFT WorksHow NFT Works
How NFT Works
101 Blockchains
 
ニュースパスのクローラーアーキテクチャとマイクロサービス
ニュースパスのクローラーアーキテクチャとマイクロサービスニュースパスのクローラーアーキテクチャとマイクロサービス
ニュースパスのクローラーアーキテクチャとマイクロサービス
mosa siru
 
UX、デザイン思考、リーンスタートアップのためのオブザベーション(観察)入門:2014年7月5日 UXD/HCD ワイワイCAFE
UX、デザイン思考、リーンスタートアップのためのオブザベーション(観察)入門:2014年7月5日 UXD/HCD ワイワイCAFEUX、デザイン思考、リーンスタートアップのためのオブザベーション(観察)入門:2014年7月5日 UXD/HCD ワイワイCAFE
UX、デザイン思考、リーンスタートアップのためのオブザベーション(観察)入門:2014年7月5日 UXD/HCD ワイワイCAFE
Yoshiki Hayama
 
もしプロダクトマネージャー・プロダクトチームにUXリサーチのメンターがついたら <レクイエム>
もしプロダクトマネージャー・プロダクトチームにUXリサーチのメンターがついたら <レクイエム>もしプロダクトマネージャー・プロダクトチームにUXリサーチのメンターがついたら <レクイエム>
もしプロダクトマネージャー・プロダクトチームにUXリサーチのメンターがついたら <レクイエム>
Yoshiki Hayama
 
技術コミュニティリーダーMicrosoft MVPの活躍に学ぶ コミュニティ活動のススメ
技術コミュニティリーダーMicrosoft MVPの活躍に学ぶ コミュニティ活動のススメ技術コミュニティリーダーMicrosoft MVPの活躍に学ぶ コミュニティ活動のススメ
技術コミュニティリーダーMicrosoft MVPの活躍に学ぶ コミュニティ活動のススメ
Rie Moriguchi
 
心理的安全性と、Veinの紹介 Psychological safety and introduction of Vein
心理的安全性と、Veinの紹介 Psychological safety and introduction of Vein心理的安全性と、Veinの紹介 Psychological safety and introduction of Vein
心理的安全性と、Veinの紹介 Psychological safety and introduction of Vein
Tokoroten Nakayama
 
グルーミングしながら進めるプロダクト開発
グルーミングしながら進めるプロダクト開発グルーミングしながら進めるプロダクト開発
グルーミングしながら進めるプロダクト開発
Takafumi ONAKA
 
45分間で「ユーザー中心のものづくり」ができるまで詰め込む
45分間で「ユーザー中心のものづくり」ができるまで詰め込む45分間で「ユーザー中心のものづくり」ができるまで詰め込む
45分間で「ユーザー中心のものづくり」ができるまで詰め込む
Yoshiki Hayama
 

What's hot (20)

The Types of Cryptocurrency Wallets
The Types of Cryptocurrency WalletsThe Types of Cryptocurrency Wallets
The Types of Cryptocurrency Wallets
 
chatgpt dalle.pptx
chatgpt dalle.pptxchatgpt dalle.pptx
chatgpt dalle.pptx
 
From gaming to the metaverse
From gaming to the metaverseFrom gaming to the metaverse
From gaming to the metaverse
 
プロダクトのフェーズとマネジメント
プロダクトのフェーズとマネジメントプロダクトのフェーズとマネジメント
プロダクトのフェーズとマネジメント
 
Specification-By-Example with Gherkin
Specification-By-Example with GherkinSpecification-By-Example with Gherkin
Specification-By-Example with Gherkin
 
ユーザーエクスペリエンスの分解
ユーザーエクスペリエンスの分解ユーザーエクスペリエンスの分解
ユーザーエクスペリエンスの分解
 
Kids coding
Kids codingKids coding
Kids coding
 
Is Metaverse The Next Big Thing?
Is Metaverse The Next Big Thing?Is Metaverse The Next Big Thing?
Is Metaverse The Next Big Thing?
 
Web3 Market Overview Global & Japan
Web3 Market Overview Global & JapanWeb3 Market Overview Global & Japan
Web3 Market Overview Global & Japan
 
お客様の目を覚ませ! ついでに自分の目も覚ませ! デザイン思考のクライアントワークのプレセールス
お客様の目を覚ませ! ついでに自分の目も覚ませ! デザイン思考のクライアントワークのプレセールスお客様の目を覚ませ! ついでに自分の目も覚ませ! デザイン思考のクライアントワークのプレセールス
お客様の目を覚ませ! ついでに自分の目も覚ませ! デザイン思考のクライアントワークのプレセールス
 
Crypto 101
Crypto 101Crypto 101
Crypto 101
 
3 Business Problems Chatbots Solve Best
3 Business Problems Chatbots Solve Best3 Business Problems Chatbots Solve Best
3 Business Problems Chatbots Solve Best
 
How NFT Works
How NFT WorksHow NFT Works
How NFT Works
 
ニュースパスのクローラーアーキテクチャとマイクロサービス
ニュースパスのクローラーアーキテクチャとマイクロサービスニュースパスのクローラーアーキテクチャとマイクロサービス
ニュースパスのクローラーアーキテクチャとマイクロサービス
 
UX、デザイン思考、リーンスタートアップのためのオブザベーション(観察)入門:2014年7月5日 UXD/HCD ワイワイCAFE
UX、デザイン思考、リーンスタートアップのためのオブザベーション(観察)入門:2014年7月5日 UXD/HCD ワイワイCAFEUX、デザイン思考、リーンスタートアップのためのオブザベーション(観察)入門:2014年7月5日 UXD/HCD ワイワイCAFE
UX、デザイン思考、リーンスタートアップのためのオブザベーション(観察)入門:2014年7月5日 UXD/HCD ワイワイCAFE
 
もしプロダクトマネージャー・プロダクトチームにUXリサーチのメンターがついたら <レクイエム>
もしプロダクトマネージャー・プロダクトチームにUXリサーチのメンターがついたら <レクイエム>もしプロダクトマネージャー・プロダクトチームにUXリサーチのメンターがついたら <レクイエム>
もしプロダクトマネージャー・プロダクトチームにUXリサーチのメンターがついたら <レクイエム>
 
技術コミュニティリーダーMicrosoft MVPの活躍に学ぶ コミュニティ活動のススメ
技術コミュニティリーダーMicrosoft MVPの活躍に学ぶ コミュニティ活動のススメ技術コミュニティリーダーMicrosoft MVPの活躍に学ぶ コミュニティ活動のススメ
技術コミュニティリーダーMicrosoft MVPの活躍に学ぶ コミュニティ活動のススメ
 
心理的安全性と、Veinの紹介 Psychological safety and introduction of Vein
心理的安全性と、Veinの紹介 Psychological safety and introduction of Vein心理的安全性と、Veinの紹介 Psychological safety and introduction of Vein
心理的安全性と、Veinの紹介 Psychological safety and introduction of Vein
 
グルーミングしながら進めるプロダクト開発
グルーミングしながら進めるプロダクト開発グルーミングしながら進めるプロダクト開発
グルーミングしながら進めるプロダクト開発
 
45分間で「ユーザー中心のものづくり」ができるまで詰め込む
45分間で「ユーザー中心のものづくり」ができるまで詰め込む45分間で「ユーザー中心のものづくり」ができるまで詰め込む
45分間で「ユーザー中心のものづくり」ができるまで詰め込む
 

Similar to Mixing Scala and Kotlin

Kotlin: Why Do You Care?
Kotlin: Why Do You Care?Kotlin: Why Do You Care?
Kotlin: Why Do You Care?
intelliyole
 
The things we don't see – stories of Software, Scala and Akka
The things we don't see – stories of Software, Scala and AkkaThe things we don't see – stories of Software, Scala and Akka
The things we don't see – stories of Software, Scala and Akka
Konrad Malawski
 
Building Mobile Apps with Android
Building Mobile Apps with AndroidBuilding Mobile Apps with Android
Building Mobile Apps with Android
Kurt Renzo Acosta
 
Anko & Karamba in Kotlin by Bapusaheb Patil
Anko & Karamba in Kotlin by Bapusaheb PatilAnko & Karamba in Kotlin by Bapusaheb Patil
Anko & Karamba in Kotlin by Bapusaheb Patil
Bapusaheb Patil
 
Why scala is not my ideal language and what I can do with this
Why scala is not my ideal language and what I can do with thisWhy scala is not my ideal language and what I can do with this
Why scala is not my ideal language and what I can do with this
Ruslan Shevchenko
 
ScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin Odersky
Typesafe
 
Power Up Your Build - Omer van Kloeten @ Wix 2018-04
Power Up Your Build - Omer van Kloeten @ Wix 2018-04Power Up Your Build - Omer van Kloeten @ Wix 2018-04
Power Up Your Build - Omer van Kloeten @ Wix 2018-04
Omer van Kloeten
 
Polyglot
PolyglotPolyglot
Polyglot
Rory Preddy
 
Intro to OOP
Intro to OOPIntro to OOP
Intro to OOP
Gant Laborde
 
3 little clojure functions
3 little clojure functions3 little clojure functions
3 little clojure functions
Diego Pacheco
 
Coscup
CoscupCoscup
Coscup
宇宣 賴
 
Design Summit - UI Roadmap - Dan Clarizio, Martin Povolny
Design Summit - UI Roadmap - Dan Clarizio, Martin PovolnyDesign Summit - UI Roadmap - Dan Clarizio, Martin Povolny
Design Summit - UI Roadmap - Dan Clarizio, Martin Povolny
ManageIQ
 
Introduction to meta-programming in scala
Introduction to meta-programming in scalaIntroduction to meta-programming in scala
Introduction to meta-programming in scala
Alessandro Marrella
 
Sync considered unethical
Sync considered unethicalSync considered unethical
Sync considered unethical
💡 Tomasz Kogut
 
Introduction to Qt
Introduction to QtIntroduction to Qt
Introduction to Qt
Puja Pramudya
 
From Android NDK To AOSP
From Android NDK To AOSPFrom Android NDK To AOSP
From Android NDK To AOSP
Min-Yih Hsu
 
Kotlin for Android Developers - 1
Kotlin for Android Developers - 1Kotlin for Android Developers - 1
Kotlin for Android Developers - 1
Mohamed Nabil, MSc.
 
Koin Quickstart
Koin QuickstartKoin Quickstart
Koin Quickstart
Matthew Clarke
 
Monix : A Birds’ eye view
Monix : A Birds’ eye viewMonix : A Birds’ eye view
Monix : A Birds’ eye view
Knoldus Inc.
 
SpringOne Platform recap 정윤진
SpringOne Platform recap 정윤진SpringOne Platform recap 정윤진
SpringOne Platform recap 정윤진
VMware Tanzu Korea
 

Similar to Mixing Scala and Kotlin (20)

Kotlin: Why Do You Care?
Kotlin: Why Do You Care?Kotlin: Why Do You Care?
Kotlin: Why Do You Care?
 
The things we don't see – stories of Software, Scala and Akka
The things we don't see – stories of Software, Scala and AkkaThe things we don't see – stories of Software, Scala and Akka
The things we don't see – stories of Software, Scala and Akka
 
Building Mobile Apps with Android
Building Mobile Apps with AndroidBuilding Mobile Apps with Android
Building Mobile Apps with Android
 
Anko & Karamba in Kotlin by Bapusaheb Patil
Anko & Karamba in Kotlin by Bapusaheb PatilAnko & Karamba in Kotlin by Bapusaheb Patil
Anko & Karamba in Kotlin by Bapusaheb Patil
 
Why scala is not my ideal language and what I can do with this
Why scala is not my ideal language and what I can do with thisWhy scala is not my ideal language and what I can do with this
Why scala is not my ideal language and what I can do with this
 
ScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin Odersky
 
Power Up Your Build - Omer van Kloeten @ Wix 2018-04
Power Up Your Build - Omer van Kloeten @ Wix 2018-04Power Up Your Build - Omer van Kloeten @ Wix 2018-04
Power Up Your Build - Omer van Kloeten @ Wix 2018-04
 
Polyglot
PolyglotPolyglot
Polyglot
 
Intro to OOP
Intro to OOPIntro to OOP
Intro to OOP
 
3 little clojure functions
3 little clojure functions3 little clojure functions
3 little clojure functions
 
Coscup
CoscupCoscup
Coscup
 
Design Summit - UI Roadmap - Dan Clarizio, Martin Povolny
Design Summit - UI Roadmap - Dan Clarizio, Martin PovolnyDesign Summit - UI Roadmap - Dan Clarizio, Martin Povolny
Design Summit - UI Roadmap - Dan Clarizio, Martin Povolny
 
Introduction to meta-programming in scala
Introduction to meta-programming in scalaIntroduction to meta-programming in scala
Introduction to meta-programming in scala
 
Sync considered unethical
Sync considered unethicalSync considered unethical
Sync considered unethical
 
Introduction to Qt
Introduction to QtIntroduction to Qt
Introduction to Qt
 
From Android NDK To AOSP
From Android NDK To AOSPFrom Android NDK To AOSP
From Android NDK To AOSP
 
Kotlin for Android Developers - 1
Kotlin for Android Developers - 1Kotlin for Android Developers - 1
Kotlin for Android Developers - 1
 
Koin Quickstart
Koin QuickstartKoin Quickstart
Koin Quickstart
 
Monix : A Birds’ eye view
Monix : A Birds’ eye viewMonix : A Birds’ eye view
Monix : A Birds’ eye view
 
SpringOne Platform recap 정윤진
SpringOne Platform recap 정윤진SpringOne Platform recap 정윤진
SpringOne Platform recap 정윤진
 

Recently uploaded

zOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL DifferenceszOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL Differences
YousufSait3
 
Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !
Marcin Chrost
 
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
Bert Jan Schrijver
 
14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision
ShulagnaSarkar2
 
UI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design SystemUI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design System
Peter Muessig
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
Grant Fritchey
 
What next after learning python programming basics
What next after learning python programming basicsWhat next after learning python programming basics
What next after learning python programming basics
Rakesh Kumar R
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
Hornet Dynamics
 
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
safelyiotech
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
ToXSL Technologies
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
Drona Infotech
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
Remote DBA Services
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
Philip Schwarz
 
All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
Alina Yurenko
 
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
XfilesPro
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
Green Software Development
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
rodomar2
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
Octavian Nadolu
 
Preparing Non - Technical Founders for Engaging a Tech Agency
Preparing Non - Technical Founders for Engaging  a  Tech AgencyPreparing Non - Technical Founders for Engaging  a  Tech Agency
Preparing Non - Technical Founders for Engaging a Tech Agency
ISH Technologies
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
ICS
 

Recently uploaded (20)

zOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL DifferenceszOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL Differences
 
Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !
 
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
 
14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision
 
UI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design SystemUI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design System
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
 
What next after learning python programming basics
What next after learning python programming basicsWhat next after learning python programming basics
What next after learning python programming basics
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
 
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
 
All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
 
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
 
Preparing Non - Technical Founders for Engaging a Tech Agency
Preparing Non - Technical Founders for Engaging  a  Tech AgencyPreparing Non - Technical Founders for Engaging  a  Tech Agency
Preparing Non - Technical Founders for Engaging a Tech Agency
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
 

Mixing Scala and Kotlin

  • 1. Mixing Scala and Kotlin March 2021 Alexey Soshin Solutions Architect
  • 2. Agenda ● What is Depop ● Why?! ● Collections ● Nullability ● Functions ● Coroutines
  • 3. Depop Building the most diverse, progressive Home of Fashion. ● Fashion marketplace app for the next generation to buy, sell and discover unique fashion. ● Community of like minded creatives, young entrepreneurs and sustainable enthusiasts who are transforming the fashion industry. ● Globally, over 26 million users on the platform in 147 countries, and 90% of our active users are under the age of 26, or Gen Z. ● As an app based platform, Depop combines the familiarity of social media with the mechanics of a resale marketplace. ● Our Mission is to Empower our community to create a new sustainable and equitable fashion system.
  • 5. Scala was there first ● Scala first appeared around 2004 ● Kotlin - 2011, out of Beta in 2016
  • 6. The Dinosaur in the Room
  • 9. Mobile Developers ❤️ Kotlin ● Kotlin is the main language on Android ● Conceptually closer to Swift than Scala ● Great for Backend For Frontend approach, where Mobile engineers control the API layer
  • 11. def doSomething(properties: Map[String, List[String]]) fun doSomething(properties: Map<String, List<String>>) Type mismatch: inferred type is kotlin.collections.Map<String, kotlin.collections.List<String>> but scala.collection.immutable.Map<String!, scala.collection.immutable.List<String!>!>! was expected 🤔 😱 Collections
  • 12. Collections def doSomething(m: Map[String, List[String]]) doSomething(mapOf("hello" to listOf("world"))) import scala.collection.JavaConverters._ def doSomething(m: java.util.Map[String, java.util.List[String]])
  • 13. Collections def doSomething(m: java.util.Map[String, java.util.List[String]]) = { val a: mutable.Map[String, util.List[String]] = m.asScala val b: collection.Map[String, List[String]] = a.mapValues(l => l.asScala.toList) doSomething(b.toMap) } def doSomething(m: Map[String, List[String]])
  • 14. Scala Option vs Kotlin nullability
  • 15. def doSomething(value: Option[String] = None) fun doSomething(value: String?) 🤔 Nullability Type mismatch: Required: Option<String!>! Found: String?
  • 16. inline fun <reified T> none() = Option.empty<T>() inline fun <reified T> some(t: T) = Option.apply(t) if (value == null) none() else some(value) private fun Any?.asOption() = if (this == null) none() else some(this) Nullability
  • 17. When a Function is not a Function
  • 18. def executeBlock(block: () => Unit) = { println("I'm going to execute it!") block() } fun executeBlock(block: () -> Unit) { println("I'm going to execute it!") block() } Functions =?
  • 19. MainKt.executeBlock(() => println("Hello from Scala")) Error: type mismatch; found : Unit (in scala) required: Unit (in kotlin) MainKt.executeBlock(() => println("hello")) Functions
  • 20. def executeBlock(block: () => Unit) = { println("I'm going to execute it!") block() } ≠ fun executeBlock(block: () -> Unit) { println("I'm going to execute it!") block() } kotlin.jvm.functions.Function0 scala.Function0 Functions
  • 21. def executeBlock(block: () => Int): Int = { println("I'm going to execute it!") block() 1 } =? fun executeBlock(block: () -> Int): Int { println("I'm going to execute it!") block() return 1 } kotlin.jvm.functions.Function0 scala.Function0 ≠ Functions
  • 22. MainKt.executeBlock(() => 1) ScalaObject.executeBlock { println("Hello from Kotlin") 1 } 👍 👍 Functions
  • 24. class AsyncKotlinClass : CoroutineScope { override val coroutineContext = Dispatchers.Default fun getCatAsync(name: String) = async { Cat(name) } suspend fun getCat(name: String): Cat { delay(100) return Cat(name) } } Coroutines
  • 25. val asyncKotlinClass = new AsyncKotlinClass() val res = asyncKotlinClass.getCatAsync("Fluffy") val cat = res.await() Error: not enough arguments for method await: (x$1: kotlin.coroutines.Continuation[_ >: Cat])Object. Unspecified value parameter x$1. val cat = res.await() Coroutines
  • 26. val asyncKotlinClass = new AsyncKotlinClass() val cat = asyncKotlinClass.getCat("Fluffy") Error: not enough arguments for method getCat: (x$1: String, x$2: kotlin.coroutines.Continuation[_ >: Cat])Object. Unspecified value parameter x$2. val cat = asyncKotlinClass.getCat("Fluffy") 😱 Coroutines
  • 28. import kotlinx.coroutines.future.* fun getCatAsync(name: String) = async { Cat(name) }.asCompletableFuture() import scala.compat.java8.FutureConverters._ val asyncKotlinClass = new AsyncKotlinClass() val res = asyncKotlinClass.getCatAsync("Fluffy") val cat = toScala(res) Coroutines
  • 29. Summary ● Reasons to adopt Kotlin in a Scala company: ○ Easier to adopt ○ Mobile Developers love it ● Reasons to keep using Scala ○ Very powerful language ○ Lots of code already written in it ● If you need to mix Scala and Kotlin ○ Easier to call Kotlin from Scala than Scala from Kotlin
  • 30. We’re Hiring! Our Blog: https://engineering.depop.com Tech careers: https://boards.greenhouse.io/depop Stay connected: https://alexey-soshin.medium.com/ https://twitter.com/alexey_soshin https://www.udemy.com/user/alexey-soshin/

Editor's Notes

  1. Hi, My name is Alexey Soshin, I’m a Solutions Architect at Depop, And today we are gonna talk about mixing Scala and Kotlin
  2. So, the agenda for this evening. First shortly about Depop Then why would we even consider mixing Scala and Kotlin And then we’ll dive into different topics. So for today I want to cover collections, handling nulls, working with functions and coroutines Sounds good?
  3. Depop is a marketplace for fashion. We have about 25 million users globally, 30 million items, and more than one hundred thousand items added every day. In terms of engineering, we are about one hundred engineers at the moment, and around 200 microservices, which are mostly written in Scala. We also have a Python monolith that we’re slowly breaking down, but I won’t talk about that today.
  4. Every time I even mention this talk, people ask me: but why would you even do that?! Well, there are a few reasons.
  5. Scala came out in 2004, so it has at least 7 to 10 years of active development on top of Kotlin. So, companies have a lot of code already written in Scala, a lot of libraries, and we want to reuse that.
  6. Now, there’s a dinosaur in the room, and it’s of course Java. But scenarios where you need to mix Scala and Java or Kotlin in Java are actually well supported. Because authors of both languages knew that Java has a huge codebase, a lot of libraries, and you want to make sure that you can make good use of them. If you wanted to throw everything mankind achieved since 95 out of the window, you could try Go instead. Okay, so why not simply keep writing everything in Scala then? There are a few reasons for that.
  7. First, Scala engineers are hard to find. There is a lot of competition on the market for them. And once you hire them, you want to put them on the most difficult tasks. But not all of the tasks in your company are difficult. So, Kotlin is a good way to diversify your codebase. https://unsplash.com/photos/dxFi8Ea670E
  8. And the reason Scala engineers are hard to find is because Scala is a hard language. I have another talk where I try to convince the audience that it’s not that hard, but in general, it is. I encountered it mainly at Wix, where we were hiring a lot of graduates. And those were top graduates from top universities. But still for a lot of them functional programming in Scala was hard. It took them months to get productive. The courses in the university are mainly C, Java, Python, and all those are mostly imperative, almost procedural languages. So again, I would advocate Kotlin for the places that have a lot of juniors and struggle to onboard them well. https://unsplash.com/photos/WiKEnlt6Z3U
  9. And finally, there’s the Backend for Frontend services. Well, hopefully I convinced you now that there are some valid reasons to mix the two. The main goal is always to reuse the Scala codebase, Scala libraries that the organisation already has, in Kotlin microservice. I’m not advocating for putting Scala and Kotlin code in the same microservice. Although Now, let’s dive into the problems, I don’t like the word challenges, that you’ll be facing. https://unsplash.com/photos/Rs9ypWXB1vE
  10. So, let’s start with something that seems relatively simple. Compatibility between data structures
  11. So, if we look at those two functions, one is written in Kotlin, another is in Scala, we, as people, can argue that their input is exactly the same. But if we tried to invoke one passing the input to the other, we would get this friendly error message. And that will be a common motive in this talk. Since we’re on JVM, it may seem like every should just work. People were asking me why was I hired as a Principal Engineer to solve those kinds of issues. Should it you know, just work? No, it shouldn’t.
  12. So, that’s what we want to do. Just call a Scala method from Kotlin that receives a map of lists of strings. For that in Scala, we’ll need the help of JavaConverters, and to have another adapter function. Notice that here w use Java Map and Java List, instead of Scala’s That’s because Kotlin has built in support for them.
  13. But that’s not all. Even with JavaConverters, we can convert Java map to Scala map, but that won’t do a deeper conversion. So then we need to iterate over map values and convert each of them to a Scala list too. But this doesn’t produce the correct map yet, so we’ll need to call toMap on a map, to get the map we want. Fun!
  14. Those two have similar meanings, both indicate possible absence of value. But how can we integrate between them?
  15. So, your Kotlin function works with something that is a String or null. And Scala likes to receives those as Option. But nullable string is clearly not on Option of a string for the compiler.
  16. Kotlin extension methods to the rescue.
  17. I guess everybody here like functional programming. Functions are great. Let’s dive into that.
  18. On the left we have a higher order function definition from Kotlin And on the right we have the same definition in Scala You need to squint really hard to notice the difference But are they really equal?
  19. So, let’s try to pass Scala lambda to this Kotlin function And we get this error, saying that Scala Unit is not a Kotlin Unit Well, in the hindsight, that totally makes sense Those are two totally different classes
  20. So, we figured out that those two are definitely not the same And we could see that even earlier, if we just had looked at the classes behind the lambdas
  21. So, we figured out that those lambdas are represented by two different classes. If we change the return type of the lambda, the problem would still be the same. Right?
  22. Turns out, they are interchangable You can pass Scala lambda to Kotlin, and you can pass Kotlin block to Scala. So, in fact, Function in Kotlin and Function in Scala are interchangeable, as long as their input and output is interchangeable.
  23. I guess everybody here like functional programming. Functions are great. Let’s dive into that.
  24. Coroutines are similar to lightweight threads or fibers. So, here we have a Kotlin class with two methods One is asynchronous method, that returns a deferred result And another is a suspending method, which is like blocking, but instead of blocking a thread, it suspends the coroutine CoroutineContext is like an execution context
  25. Let’s try using the first one from Scala It even may look like it’s working, until you try to get the result Await() method has no arguments, but compiler demands that we pass it something called continuation as the first parameter. Doesn’t make any sense.
  26. Ok, let’s try to invoke second method. It should be simple. Now although we have one argument, we’re required to pass Continuation as a second parameter. Again, no luck.
  27. Trying to complete Scala future from Kotlin won’t work, because it’ll need execution context. So, you’ll be passing the gorilla and the entire jungle, when you just wanted the banana. But there’s something else that will help us. And this “something else” is weirdly enough - Java
  28. We can import coroutines/future package in Kotlin, and we’ll get a method that converts Kotlin Deferred value into CompletableFuture Now, Java CompletableFuture can be converted to Scala future. Or to any other future, for this matter.