Slide 1

Slide 1 text

Property and Backing Field @MoyuruAizawa

Slide 2

Slide 2 text

Ѫᖒ๖ (Moyuru Aizawa) - Kotlin engineer at CyberAgent, Inc. - FRESH! lvla0805 MoyuruAizawa

Slide 3

Slide 3 text

Property?

Slide 4

Slide 4 text

class Person { val firstName: String val lastName: String … } Property

Slide 5

Slide 5 text

class Person { val firstName: String val lastName: String … } Property

Slide 6

Slide 6 text

Kotlin compiler generates Backing Fields and Accessors

Slide 7

Slide 7 text

public final class Person { @NotNull private final String firstName; @NotNull private final String lastName; … @NotNull public final String getFirstName() { return this.firstName; } @NotNull public final String getLastName() { return this.lastName; } } Backing Fields and Accessors

Slide 8

Slide 8 text

public final class Person { @NotNull private final String firstName; @NotNull private final String lastName; … @NotNull public final String getFirstName() { return this.firstName; } @NotNull public final String getLastName() { return this.lastName; } } Backing Fields

Slide 9

Slide 9 text

public final class Person { @NotNull private final String firstName; @NotNull private final String lastName; … @NotNull public final String getFirstName() { return this.firstName; } @NotNull public final String getLastName() { return this.lastName; } } Accessors

Slide 10

Slide 10 text

public final class Person { @NotNull private final String firstName; @NotNull private final String lastName; … @NotNull public final String getFirstName() { return this.firstName; } @NotNull public final String getLastName() { return this.lastName; } } Backing Fields

Slide 11

Slide 11 text

Declare Custom Accessors

Slide 12

Slide 12 text

class Person { val firstName: String val lastName: String val fullName get() = "$firstName $lastName" … } Declare custom accessors

Slide 13

Slide 13 text

class Person { val firstName: String val lastName: String val fullName get() = "$firstName $lastName" … } Declare custom accessors

Slide 14

Slide 14 text

class Person { val firstName: String val lastName: String val fullName get() = "$firstName $lastName" … } Declare custom accessors

Slide 15

Slide 15 text

class Person { val firstName: String val lastName: String val fullName get() = "$firstName $lastName" … } person.fullName Declare custom accessors

Slide 16

Slide 16 text

public final class Person { @NotNull private final String firstName; @NotNull private final String lastName; … @NotNull public final String getFirstName() { return this.firstName; } @NotNull public final String getLastName() { return this.lastName; } @NotNull public final String getFullName() { return this.firstName + " " + this.lastName; } } Declare custom accessors

Slide 17

Slide 17 text

class Person { val firstName: String val lastName: String val fullName get() = "$firstName $lastName" var age: Int set(value) { require(value >= 0) field = value } … } Declare custom accessors

Slide 18

Slide 18 text

class Person { val firstName: String val lastName: String val fullName get() = "$firstName $lastName" var age: Int set(value) { require(value >= 0) field = value } … } Declare custom accessors

Slide 19

Slide 19 text

class Person { val firstName: String val lastName: String val fullName get() = "$firstName $lastName" var age: Int set(value) { require(value >= 0) field = value } … } Declare custom accessors

Slide 20

Slide 20 text

class Person { val firstName: String val lastName: String val fullName get() = "$firstName $lastName" var age: Int set(value) { require(value >= 0) field = value } … } Declare custom accessors

Slide 21

Slide 21 text

class Person { val firstName: String val lastName: String val fullName get() = "$firstName $lastName" var age: Int set(value) { require(value >= 0) age = value } … } Declare custom accessors Stack Overflow

Slide 22

Slide 22 text

class Person { val firstName: String val lastName: String val fullName get() = "$firstName $lastName" var age: Int set(value) { require(value >= 0) field = value } … } person.age = 26 Declare custom accessors

Slide 23

Slide 23 text

public final class Person { … private int age; … public final int getAge() { return this.age; } public final void setAge(int value) { required(value) this.age = value; } } Declare custom accessors

Slide 24

Slide 24 text

public final class Person { … private int age; … public final int getAge() { return this.age; } public final void setAge(int value) { required(value) this.age = value; } } Declare custom accessors

Slide 25

Slide 25 text

public final class Person { … private int age; … public final int getAge() { return this.age; } public final void setAge(int value) { required(value) this.age = value; } } Declare custom accessors

Slide 26

Slide 26 text

var [: ] [= ] [] [] Declare custom accessors

Slide 27

Slide 27 text

Properties without Backing Fields

Slide 28

Slide 28 text

class NoBackingField { val property get() = "property" } No Backing Fields

Slide 29

Slide 29 text

public final class NoBackingField { @NotNull public final String getProperty() { return "property"; } } No Backing Fields

Slide 30

Slide 30 text

Property without Backing Field ≒ Function

Slide 31

Slide 31 text

Interface can have Properties without Backing Fields

Slide 32

Slide 32 text

interface User { val id: Long val name: String } data class Follower(override val id: Long, override val name: String): User data class Me(override val id: Long, override val name: String, val email: String): User Interface can have Properties without Backing Fields

Slide 33

Slide 33 text

interface User { val id: Long val name: String } data class Follower(override val id: Long, override val name: String): User data class Me(override val id: Long, override val name: String, val email: String): User Interface can have Properties without Backing Fields

Slide 34

Slide 34 text

interface User { val id: Long val name: String } data class Follower(override val id: Long, override val name: String): User data class Me(override val id: Long, override val name: String, val email: String): User Interface can have Properties without Backing Fields

Slide 35

Slide 35 text

Extend a class with new Properties without Backing Fields

Slide 36

Slide 36 text

val View.isVisible get() = visibility == View.VISIBLE val Activity.applicationComponent get() = (application as CustomApplication).component Extend a class with new Properties without Backing Fields

Slide 37

Slide 37 text

val View.isVisible get() = visibility == View.VISIBLE val Activity.applicationComponent get() = (application as CustomApplication).component Extend a class with new Properties without Backing Fields

Slide 38

Slide 38 text

val View.isVisible get() = visibility == View.VISIBLE val Activity.applicationComponent get() = (application as CustomApplication).component Extend a class with new Properties without Backing Fields

Slide 39

Slide 39 text

‣ Kotiln generates Backing Fields and Accessors ‣ You can declare custom Accessors ‣ Interface can have properties without Backing Fields ‣ You can extend a class with new Properties without Backing Fields

Slide 40

Slide 40 text

Thank you