Slide 1

Slide 1 text

The Lighthouse Language

Slide 2

Slide 2 text

Hi There! ● Software Developer for three years ○ Medical Research ○ eCommerce ● Android Developer at heart and profession ● Software Developer at Gett ● Co-Organizer KotlinTLV

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

So what is Kotlin? ● Language developed by Jetbrains (IntelliJ) ● Open sourced in 2011, Kotlin 1.0 on Feb 15th 2016 ● 100% interoperable with Java ● First class citizen language announcement in I/O 2017

Slide 5

Slide 5 text

Do we seriously need another language? Hear me out :)

Slide 6

Slide 6 text

Do we seriously need another language? ● Costs nothing to adopt ● Concise ● Negligible Runtime overhead ● Solves the Nullability Problem ● Awesome and Modern

Slide 7

Slide 7 text

Concise? public class JavaPerson { private int age; private String name; private String address; public JavaPerson(int age, String name, String address) { this.age = age; this.name = name; this.address = address; } ...

Slide 8

Slide 8 text

Concise..? public void setAddress(String address) { this.address = address; } public void setAge(int age) { this.age = age; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public String getAddress() { return address; } public String getName() { return name; }

Slide 9

Slide 9 text

Yikes..? @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; JavaPerson that = (JavaPerson) o; if (age != that.age) return false; if (name != null ? !name.equals(that.name) : that.name != null) return false; return address != null ? address.equals(that.address) : that.address == null; } @Override public int hashCode() { int result = age; result = 31 * result + (name != null ? name.hashCode() : 0); result = 31 * result + (address != null ? address.hashCode() : 0); return result; } }

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Good class KotlinPerson(var age: Int, var name: String, var address: String)

Slide 12

Slide 12 text

Better data class KotlinPerson(var age: Int, var name: String, var address: String)

Slide 13

Slide 13 text

Today ● Variables ● Functions ● Extension Functions ● Null Safety ● Intro to Object-Oriented Programming

Slide 14

Slide 14 text

A few ground rules before we start ● No static* ● Everything that worked in Java will still work ● No semicolons needed* * Except under adult supervision...

Slide 15

Slide 15 text

val/var name = "Hanan" // Compiler will complain val name: String = "Yarkoni" // public final String name = “Yarkoni” val name = "Yarkoni" // Type Inference

Slide 16

Slide 16 text

val/var var name: String = "Yarkoni" public String name = “Yarkoni” var name = "Yarkoni" // Type Inference name = "Hanan" // Compiler will be fine

Slide 17

Slide 17 text

Statically Typed Language var name = "Yarkoni" name = 5 // Compiler will complain name = "Hanan" // Compiler will be fine

Slide 18

Slide 18 text

100% Java Interop val sb : StringBuilder = StringBuilder("Here") // Java StringBuilder* sb.append(" is some").append(" text!") // Java StringBuilder methods println(sb) // Wrapper for System.out.println

Slide 19

Slide 19 text

Questions?

Slide 20

Slide 20 text

Fun(ctions: Any)

Slide 21

Slide 21 text

Functions fun maximum(first: Int, second: Int): Int { if (first > second) { return first } else { return second } }

Slide 22

Slide 22 text

Functions - Default Values fun maximum(first: Int = 3, second: Int = 4): Int { if (first > second) { return first } else { return second } }

Slide 23

Slide 23 text

Default Values - Java 7 public int maximum() { return maximum(3, 4); } public int maximum(int first) { return maximum(first, 4); } public int maximumFirstDefault(int second) { return maximum(3, second); } public int maximum(int first, int second) { return first > second ? first : second; }

Slide 24

Slide 24 text

Function Calling // Regular call with parameters maximum(7, 6) // Named parameters maximum(first = 7, second = 6) maximum(second = 6, first = 7) maximum(second = 6)

Slide 25

Slide 25 text

Functions - Concise fun maximum(first: Int = 3, second: Int = 4): Int { if (first > second) { return first } else { return second } }

Slide 26

Slide 26 text

Functions - Concise fun maximum(first: Int = 3, second: Int = 4): Int = if (first > second) { first } else { second }

Slide 27

Slide 27 text

Functions - Concise fun maximum(first: Int = 3, second: Int = 4): Int = if (first > second) first else second

Slide 28

Slide 28 text

Functions - Concise fun maximum(first: Int = 3, second: Int = 4) = if (first > second) first else second

Slide 29

Slide 29 text

Extension Functions ● Adopted from C# and Gosu ● “Extension without Inheritance” ● Basically “utils” functions

Slide 30

Slide 30 text

Extension Functions fun Int.maximum(other: Int): Int = if (this > other) this else other public static int maximum(int $receiver, int other) { if ($receiver > other) { return $receiver; } else { return other; } }

Slide 31

Slide 31 text

Extension Functions fun Int.maximum(other: Int): Int = if (this > other) this else other 7.maximum(6)

Slide 32

Slide 32 text

Other Function Features ● Infix - no . needed ● Operator - plus() is + ● Inline (noinline) - Inlines a function ● Functions within functions - double the fun!

Slide 33

Slide 33 text

Questions?

Slide 34

Slide 34 text

Safety?

Slide 35

Slide 35 text

Null Safety String amINull = "Not Null"; // Some time passes amINull = null; // Uh Oh // Some time passes amINull.equals("Null");

Slide 36

Slide 36 text

● An object reference in Java may or may not be null ● Accessing that reference at compile is ok ● Accessing that reference at runtime: ○ May be ok ○ May crash the system Nullability Problem Defined https://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare

Slide 37

Slide 37 text

● Java 8’s solution - Optional wrapper ○ Grammar (.ofNullable, .of) ○ I have to write down Optional?? ○ I have to ask isPresent() and then get()???? ● Kotlin’s solution - Built-in Nullable type Nullability Solution

Slide 38

Slide 38 text

Nullable Type val notNullString: String = "not null" val nullString: String? = null

Slide 39

Slide 39 text

Nullable Type - Usage nullString?.equals("Other string") // Safe nullString!!.equals("Other String") // Unsafe

Slide 40

Slide 40 text

Nullable Type - Chaining nullString?.toDoubleOrNull()?.compareTo(1) String nullString = ""; if (nullString != null) { if (Double.valueOf(nullString) != null) { return Double.valueOf(nullString).compareTo(1.0); } } return null;

Slide 41

Slide 41 text

Elvis Operator val result = nullString?.toDoubleOrNull()?.equals(1) ?: false

Slide 42

Slide 42 text

Questions?

Slide 43

Slide 43 text

Object-Oriented Programming

Slide 44

Slide 44 text

Object Oriented Programming - Rules ● Interfaces may extend other interfaces ● Classes my extend one other class ● Classes may implement as many interfaces as needed

Slide 45

Slide 45 text

Kotlin Rules ● Enums are called enum classes ● Abstract is a soft keyword, but abstract exists ● All objects must be fully initialized at creation ● Java may extend Kotlin ● Kotlin may extend Java

Slide 46

Slide 46 text

Open/Override class CodingExample { fun writeCode() = "I am writing code!" }

Slide 47

Slide 47 text

Open/Override open class CodingExample { open fun writeCode() = "I am writing code!" } class KotlinExample: CodingExample() { override fun writeCode() = "I am writing Kotlin Code" }

Slide 48

Slide 48 text

Open/Override open class CodingExample { open fun writeCode() = "I am writing code!" } class KotlinExample: CodingExample() { override fun writeCode() = "${super.writeCode()} I am writing Kotlin Code" }

Slide 49

Slide 49 text

Inheritance Example interface Person { fun fullName(): String } abstract class Bob implements Person { public abstract void fullName(); public String sayName() { return "My Name Is Bob"; } }

Slide 50

Slide 50 text

Inheritance Example interface Person { fun fullName(): String } abstract class Bob(open val firstName: String): Person { abstract fun fullName() fun sayName(): String = "My Name Is Bob" }

Slide 51

Slide 51 text

Inheritance Example abstract class Bob(open val firstName: String): Person { abstract fun fullName(): String //can be omitted fun sayName(): String = "My Name Is Bob" }

Slide 52

Slide 52 text

Inheritance Example - Continued class BobDylan: Bob("Bob") { // String interpolation is fun override fun fullName(): String = "${sayName()} Dylan" } abstract class Bob(open val firstName: String): Person { abstract fun fullName(): String //can be omitted fun sayName(): String = "My Name Is Bob" }

Slide 53

Slide 53 text

Inheritance Example - Continued class BobDole(override val firstName: String = "Bob", val lastName: String = "Dole"): Bob(firstName) { override fun fullName(): String = "I'm Bob Dole" } abstract class Bob(open val firstName: String): Person { abstract fun fullName(): String //can be omitted fun sayName(): String = "My Name Is Bob" }

Slide 54

Slide 54 text

Some Additions

Slide 55

Slide 55 text

Data Classes data class Elvis(val firstName: String = "Elvis", val lastName: String = "Presley") open data class Elvis(val firstName: String = "Elvis", val lastName: String = "Presley") Compiler will complain!

Slide 56

Slide 56 text

Sealed Classes sealed class Person(val firstName: String, val lastName: String) { class BobDylan: Person("Bob", "Dylan") class BobDole(val age: Int, fName: String, lName: String) : Person(fName, lName) }

Slide 57

Slide 57 text

Smart Casting fun translatePerson(person: Person): String = when (person) { is Person.BobDylan -> { "${person.firstName} ${person.lastName}" } is Person.BobDole -> { "${person.fName} ${person.lName}, of age ${person.age}" } }

Slide 58

Slide 58 text

Questions?

Slide 59

Slide 59 text

Thank You!