Kotlin: Let's Make Android Great Again

  • Published on
    23-Jan-2018

  • View
    1.429

  • Download
    0

Transcript

  • ExtendedSeoul

  • ExtendedSeoul

  • Statically typed programming language for the JVM, Android and the browser

    100% interoperable with Java

  • ExtendedSeoul

  • ExtendedSeoulExtendedSeoul

  • ExtendedSeoulExtendedSeoul

  • ExtendedSeoulExtendedSeoul

  • ExtendedSeoul

  • ExtendedSeoul

  • ExtendedSeoul

  • ExtendedSeoulExtendedSeoul

  • ExtendedSeoul

  • ExtendedSeoul

  • ExtendedSeoul

  • ExtendedSeoulExtendedSeoul

  • ExtendedSeoul

  • MyActivity.java

    btnDoIt = (Button) findViewById(R.id.btn_do_it);

  • MyActivity.java

    btnDoIt = (Button) findViewById(R.id.btn_do_it); btnDoIt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO } });

  • MyActivity.java

    btnDoIt = (Button) findViewById(R.id.btn_do_it); btnDoIt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO } });

  • WithRxJava.java

    Observable.fromArray(1, 2, 3, 4, 5) .map(new Function() { @Override public String apply(Integer param) { return "A" + param; } });

  • WithRxJava.java

    Observable.fromArray(1, 2, 3, 4, 5) .map(new Function() { @Override public String apply(Integer param) { return "A" + param; } });

  • WithRxJava.java

    Observable.fromArray("1", "2", "3", "4", "5") .map(new Function() { @Override public String apply(Integer param) { return "A" + param; } });

  • WithRxJava.java

    Observable.fromArray("1", "2", "3", "4", "5") .map(new Function() { @Override public String apply(Integer param) { return "A" + param; } });

  • WithRxJava.java

    Observable.fromArray("1", "2", "3", "4", "5") .map(new Function() { @Override public String apply(String param) { return "A" + param; } });

  • User.java

    String getAddress() { ... }

    User user = getCurrentUser(); user.getAddress().toUpperCase();

  • ExtendedSeoul

    I call it my billion-dollar mistake. This has led to innumerable errors probably caused a billion dollars of pain and damage in the last forty years.

    ExtendedSeoul

    - Tony Hoare

  • User.java

    String getAddress() { return this.address; }

    User user = getCurrentUser(); user.getAddress().toUpperCase();

  • User.java

    String getAddress() { return null == this.address ? "" : this.address; }

    User user = getCurrentUser(); user.getAddress().toUpperCase();

  • User.java

    @Nullable String getAddress() { return this.address; }

    User user = getCurrentUser(); user.getAddress().toUpperCase();

  • User.java

    @NonNull String getAddress() { return null == this.address ? "" : this.address; }

    User user = getCurrentUser(); user.getAddress().toUpperCase();

  • ExtendedSeoul

  • MyActivity.java

    Button btnFoo; TextView tvBar;

    @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

    btnFoo = (Button) findViewById(R.id.btn_foo); tvBar = (TextView) findViewById(R.id.tv_bar); ... }

  • MyActivity.java

    Button btnFoo; TextView tvBar;

    @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

    btnFoo = (Button) findViewById(R.id.btn_foo); tvBar = (TextView) findViewById(R.id.tv_bar); ... }

  • Intents.java

    Intent intent = new Intent(this, OtherActivity.class); intent.putExtra("id", 10L); intent.setFlag(Intent.FLAG_ACTIVITY_SINGLE_TOP); startActivity(intent);

  • ExtendedSeoulExtendedSeoul

  • MyActivity.java

    btnDoIt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO } });

  • MyActivity.kt

    btnDoIt.setOnClickListener(object : View.OnClickListener() { override fun onClick(v: View) { // TODO } })

  • MyActivity.kt

    btnDoIt.setOnClickListener({ v: View -> // TODO })

  • MyActivity.kt

    btnDoIt.setOnClickListener({ v -> // TODO })

  • MyActivity.kt

    btnDoIt.setOnClickListener({ it.context // View.context })

  • MyActivity.kt

    btnDoIt.setOnClickListener { it.context // View.context }

  • StandardLibrary.kt

    val people : List = ..

    people.filter { it.startsWith('S') } .filter { it.length < 10 } .onEach { it.toUpperCase() } .forEach { println(it) }

  • StandardLibrary.kt

    val people : List = ..

    people.filter { it.startsWith('S') } .filter { it.length < 10 } .onEach { String::toUpperCase() } .forEach { ::println }

  • StandardLibrary.kt

    val people : List = .. people.stream() .filter { it.startsWith('S') } .filter { it.length < 10 } .map(String::toUpperCase) .forEach(::println)

  • User.java

    @Nullable String getAddress() { return this.address; }

    User user = getCurrentUser(); user.getAddress().toUpperCase();

  • User.java

    // class User var address: String? = null

    //// val user : User = getCurrentUser() user.address?.toUpperCase()

  • User.java

    // class User var address: String? = null

    //// val user : User = getCurrentUser() user.address?.toUpperCase()

  • User.java

    // class User var address: String? = null

    //// val user : User = getCurrentUser() user.address?.toUpperCase()

  • User.java

    // class User var address: String? = null

    //// val user = getCurrentUser() user.address?.toUpperCase()

  • User.java

    @NonNull String getAddress() { return null == this.address ? "" : this.address; }

    User user = getCurrentUser(); user.getAddress().toUpperCase();

  • User.java

    // class User var address: String? = null get() = field ?: ""

    //// val user = getCurrentUser() user.address.toUpperCase()

  • User.java

    // class User var address: String? = null get() = field ?: ""

    //// val user = getCurrentUser() user.address.toUpperCase()

  • User.java

    // class User var address: String? = null get() = field ?: ""

    //// val user = getCurrentUser() user.address.toUpperCase()

  • MyActivity.java

    Button btnFoo; TextView tvBar;

    @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

    btnFoo = (Button) findViewById(R.id.btn_foo); tvBar = (TextView) findViewById(R.id.tv_bar); ... }

  • MyActivity.kt

    override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main)

    btn_foo // (Button) findViewById(R.id.btn_foo) tv_bar // (TextView) findViewById(R.id.tv_bar) ... }

  • Intents.java

    Intent intent = new Intent(this, OtherActivity.class); intent.putExtra("id", 10L); intent.setFlag(Intent.FLAG_ACTIVITY_SINGLE_TOP); startActivity(intent);

  • Intents.kt

    val intent = Intent(this, OtherActivity::class.java) intent.putExtra("id", 10L) intent.setFlag(Intent.FLAG_ACTIVITY_SINGLE_TOP) startActivity(intent)

  • Intents.kt

    startActivity( intentFor("id" to 10L).singleTop())

  • Collections.kt

    val mutableList : MutableList = mutableListOf(foo", "bar", "baz") mutableList.add("fizz")

    val immutableList : List = listOf("foo", "bar", "baz") immutableList.add("fizz")

  • Collections.kt

    val mutableList : MutableList = mutableListOf(foo", "bar", "baz") mutableList.add("fizz")

    val immutableList : List = listOf("foo", "bar", "baz") immutableList.add("fizz")

  • Collections.kt

    val emptyStringList = listOf() val cities = listOf("Seoul", "Busan") val mutableCities = mutableListOf("Seoul, Busan")

    val emptyStringSet = setOf() val cities = setOf("Seoul", "Busan") val mutableCities = mutableSetOf("Seoul, Busan")

  • Collections.kt

    val pair : Pair = Pair("Seoul", "SEO")

  • Collections.kt

    val pair : Pair = Pair("Seoul", "SEO")

    // with stdlib val pair : Pair = "Seoul" to "SEO"

  • Person.java

    public class Person { String name; String address;

    Person(String name, String address) { this.name = name; this.address = address; }

    public String getAddress() { return address; }

    .. }

  • Person.javapublic class Person {

    String name;

    String address;

    Person(String name, String address) { this.name = name; this.address = address; }

    public String getAddress() { return address; }

    public void setAddress(String address) { this.address = address; }

    @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false;

    Person person = (Person) o;

    if (name != null ? !name.equals(person.name) : person.name != null) return false; return address != null ? address.equals(person.address) : person.address == null;

    }

    @Override public int hashCode() { int result = name != null ? name.hashCode() : 0; result = 31 * result + (address != null ? address.hashCode() : 0); return result; } }

  • Person.kt

    data class Person(val name: String, val address: String)

  • MyToast.kt

    Toast.makeText(applicationContext, "Hello, Kotlin!", Toast.LENGTH_SHORT).show()

  • MyToast.kt

    // Define an extension function on Context fun Context.toast(message: String) { Toast.makeText(this.applicationContext, message, Toast.LENGTH_SHORT).show() }

    // available in class Context and its descendants toast("Hello, Kotlin!")

  • ExtendedSeoul

  • ExtendedSeoul

  • ExtendedSeoul

  • ExtendedSeoul

    ExtendedSeoul