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
Data classes in Kotlin
Search
Harri Kirik
April 02, 2018
Programming
0
29
Data classes in Kotlin
5-minute talk about data classes in Kotlin
Harri Kirik
April 02, 2018
Tweet
Share
More Decks by Harri Kirik
See All by Harri Kirik
Secure programming techniques: Mobile Development Security guest lecture
harri35
0
78
Support for HSM-like capabilities in Android
harri35
0
130
Why doesn't my in-app QR code work (on location)?
harri35
0
27
Git merge-base
harri35
1
67
Smoke testing your library
harri35
0
25
Collections in Kotlin
harri35
0
33
How to do delegation in Kotlin
harri35
0
31
Two-factor authentication at GDG Riga
harri35
0
70
Two-factor authentication at GDG Tartu
harri35
0
52
Other Decks in Programming
See All in Programming
Compose Hot Reload is here, stop re-launching your apps! (Android Makers 2025)
zsmb
1
580
Amazon CloudWatchの地味だけど強力な機能紹介!
itotsum
0
200
ComposeでのPicture in Picture
takathemax
0
130
Unlock the Potential of Swift Code Generation
rockname
0
280
note の Elasticsearch 更新系を支える技術
tchov
1
290
設計の本質:コード、システム、そして組織へ / The Essence of Design: To Code, Systems, and Organizations
nrslib
10
3.5k
AIコーディングの理想と現実
tomohisa
33
36k
The Nature of Complexity in John Ousterhout’s Philosophy of Software Design
philipschwarz
PRO
0
150
Contribute to Comunities | React Tokyo Meetup #4 LT
sasagar
0
580
カウシェで Four Keys の改善を試みた理由
ike002jp
1
110
七輪ライブラリー: Claude AI で作る Next.js アプリ
suneo3476
1
140
iOSアプリで測る!名古屋駅までの 方向と距離
ryunakayama
0
130
Featured
See All Featured
Automating Front-end Workflow
addyosmani
1370
200k
Unsuck your backbone
ammeep
670
57k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
29
1.6k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
26k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
23
2.7k
Statistics for Hackers
jakevdp
798
220k
Git: the NoSQL Database
bkeepers
PRO
430
65k
We Have a Design System, Now What?
morganepeng
52
7.5k
Making the Leap to Tech Lead
cromwellryan
133
9.2k
Code Reviewing Like a Champion
maltzj
523
40k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
160
15k
YesSQL, Process and Tooling at Scale
rocio
172
14k
Transcript
designing & developing for mobile Demoday, 02/04/2018 Harri Kirik, engineer
Kotlin: Data classes
Kotlin KOTLIN: DATA CLASSES #kotlin
Classes to hold data KOTLIN: DATA CLASSES #dataclasses
Usually in Java .. KOTLIN: DATA CLASSES #kotlin
KOTLIN: DATA CLASSES public class Person { private String customerEmail;
private String customerPhoneNr; private String customerSurname; private String customerSurnameReading; private String customerGivenName; private String customerGivenNameReading; private String customerCompany; private String customerCompanyDepartment; public Person() { } public void setCustomerEmail(String customerEmail) { this.customerEmail = customerEmail; } public void setCustomerPhoneNr(String customerPhoneNr) { this.customerPhoneNr = customerPhoneNr; } #demoday
KOTLIN: DATA CLASSES public void setCustomerSurname(String customerSurname) { this.customerSurname =
customerSurname; } public void setCustomerSurnameReading(String customerSurnameReading) { this.customerSurnameReading = customerSurnameReading; } public void setCustomerGivenName(String customerGivenName) { this.customerGivenName = customerGivenName; } public void setCustomerGivenNameReading(String customerGivenNameReading) { this.customerGivenNameReading = customerGivenNameReading; } public void setCustomerCompany(String customerCompany) { this.customerCompany = customerCompany; } public void setCustomerCompanyDepartment(String customerCompanyDepartment) { this.customerCompanyDepartment = customerCompanyDepartment; } #demoday
KOTLIN: DATA CLASSES @Override public boolean equals(Object o) { if
(this == o) return true; if (o == null || getClass() != o.getClass()) return false; Person person = (Person) o; if (customerEmail != null ? !customerEmail.equals(person.customerEmail) : person.customerEmail != null) return false; if (customerPhoneNr != null ? !customerPhoneNr.equals(person.customerPhoneNr) : person.customerPhoneNr != null) return false; if (customerSurname != null ? !customerSurname.equals(person.customerSurname) : person.customerSurname != null) return false; if (customerSurnameReading != null ? !customerSurnameReading.equals(person.customerSurnameReading) : person.customerSurnameReading != null) return false; if (customerGivenName != null ? !customerGivenName.equals(person.customerGivenName) : person.customerGivenName != null) return false; if (customerGivenNameReading != null ? !customerGivenNameReading.equals(person.customerGivenNameReading) : person.customerGivenNameReading != null) return false; if (customerCompany != null ? !customerCompany.equals(person.customerCompany) : person.customerCompany != null) return false; return customerCompanyDepartment != null ? customerCompanyDepartment.equals(person.customerCompanyDepartment) : person.customerCompanyDepartment == null; } #demoday
KOTLIN: DATA CLASSES @Override public int hashCode() { int result
= customerEmail != null ? customerEmail.hashCode() : 0; result = 31 * result + (customerPhoneNr != null ? customerPhoneNr.hashCode() : 0); result = 31 * result + (customerSurname != null ? customerSurname.hashCode() : 0); result = 31 * result + (customerSurnameReading != null ? customerSurnameReading.hashCode() : 0); result = 31 * result + (customerGivenName != null ? customerGivenName.hashCode() : 0); result = 31 * result + (customerGivenNameReading != null ? customerGivenNameReading.hashCode() : 0); result = 31 * result + (customerCompany != null ? customerCompany.hashCode() : 0); result = 31 * result + (customerCompanyDepartment != null ? customerCompanyDepartment.hashCode() : 0); return result; } #demoday
KOTLIN: DATA CLASSES @Override public String toString() { return "Person{"
+ "customerEmail='" + customerEmail + '\'' + ", customerPhoneNr='" + customerPhoneNr + '\'' + ", customerSurname='" + customerSurname + '\'' + ", customerSurnameReading='" + customerSurnameReading + '\'' + ", customerGivenName='" + customerGivenName + '\'' + ", customerGivenNameReading='" + customerGivenNameReading + '\'' + ", customerCompany='" + customerCompany + '\'' + ", customerCompanyDepartment='" + customerCompanyDepartment + '\'' + '}'; } } #demoday
Easy to generate, but tedious KOTLIN: DATA CLASSES #dataclasses
Kotlin can do better KOTLIN: DATA CLASSES #dataclasses
KOTLIN: DATA CLASSES data class Person(val customerEmail: String, val customerPhoneNr:
String, val customerSurname: String, val customerSurnameReading: String, val customerGivenName: String, val customerGivenNameReading: String, val customerCompany: String, val customerCompanyDepartment: String) #demoday
Autogenerated: 1. equals()/hashCode() pair; 2. toString() 3. componentN() functions 4.
copy() function KOTLIN: DATA CLASSES #dataclasses
conponentN() functions KOTLIN: DATA CLASSES #dataclasses
KOTLIN: DATA CLASSES val person = Person( "harri.kirik@lab.mobi", // <-
getComponent1() "+37256240131", //<- getComponent2() "Kirik", //<- getComponent3() "Kirik", //<- getComponent4() "Harri", //<- getComponent5() "Harri", //<- getComponent6() "Mobi Lab", //<- getComponent7() "Development"); //<- getComponent1() val (_, _,surname, _, givenName) = person // (C1), (C2), C3, C4 #demoday
copy() function KOTLIN: DATA CLASSES #dataclasses
KOTLIN: DATA CLASSES val person = Person("harri.kirik@lab.mobi", "+37256240131", "Kirik", "Kirik",
"Harri", "Harri", "Mobi Lab", "Development"); val personFired = person.copy( customerEmail = "harri35@gmail.com", customerCompany = "N/A", customerCompanyDepartment = "N/A") #demoday
http:// lab.mobi designing & developing for mobile thanks Questions?