Slide 1

Slide 1 text

FROM JAVA TO Gerard Paligot

Slide 2

Slide 2 text

WHAT’S KOTLIN?

Slide 3

Slide 3 text

WHAT’S KOTLIN? - Started in mid 2010 - Avoid huge Java codebase - Want a modern, expressive language - Easy to introduce in the existing environment Presentation

Slide 4

Slide 4 text

WHAT’S KOTLIN? - Full Java interoperability - Compile as fast as Java - More concise than Java - Prevent more kinds of errors than Java - Was simpler than Scala Design Goals

Slide 5

Slide 5 text

WHY USE KOTLIN? - In a future, will replace Java as first language - High adoption of the Android community

Slide 6

Slide 6 text

WHY USE KOTLIN? High adoption of the Android community

Slide 7

Slide 7 text

WHY USE KOTLIN? - In a future, will replace Java as first language - High adoption of the Android community - Easy learning for iOS and Android developers - Null safety - Compilation speed

Slide 8

Slide 8 text

WHY USE KOTLIN? Compilation speed

Slide 9

Slide 9 text

WHY USE KOTLIN? - In a future, will replace Java as first language - High adoption of the Android community - Easy learning for iOS and Android developers - Null safety - Compilation speed - Less code is better!

Slide 10

Slide 10 text

HOW TO START? - Experimenting with a legacy Java/Android project - Run automatic conversion to Kotlin and learn what changed - Practice more and inform what’s new at each new Kotlin version

Slide 11

Slide 11 text

HOW TO START // Top level build.gradle file buildscript { ext.kotlin_version = '1.2.30' // ... dependencies { // ... classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } // build.gradle file of your app module // ... apply plugin: 'kotlin-android' // ... dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" // ... }

Slide 12

Slide 12 text

HOW TO START // Top level build.gradle file buildscript { ext.kotlin_version = '1.2.30' // ... dependencies { // ... classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } // build.gradle file of your app module // ... apply plugin: 'kotlin-android' // ... dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" // ... }

Slide 13

Slide 13 text

HOW TO START // Top level build.gradle file buildscript { ext.kotlin_version = '1.2.30' // ... dependencies { // ... classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } // build.gradle file of your app module // ... apply plugin: 'kotlin-android' // ... dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" // ... }

Slide 14

Slide 14 text

JAVA TO KOTLIN… JAVA STYLE

Slide 15

Slide 15 text

JAVA TO KOTLIN… JAVA STYLE public class MyFragment extends Fragment { private static final String KEY_TITLE = "KEY_TITLE"; private static final String KEY_MESSAGE = "KEY_MESSAGE"; public static MyFragment newInstance(String newTitle, String newMessage) { Bundle args = new Bundle(); args.putString(KEY_TITLE, newTitle); args.putString(KEY_MESSAGE, newMessage); MyFragment fragment = new MyFragment(); fragment.setArguments(args); return fragment; } }

Slide 16

Slide 16 text

JAVA TO KOTLIN… JAVA STYLE public class MyFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.my_fragment, container); } }

Slide 17

Slide 17 text

JAVA TO KOTLIN… JAVA STYLE public class MyFragment extends Fragment { private TextView mTitleTextView; private TextView mMessageTextView; private Button mSubmitButton; @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); mTitleTextView = view.findViewById(R.id.titleTextView); mTitleTextView.setText(getArguments().getString(KEY_TITLE)); mMessageTextView = view.findViewById(R.id.messageTextView); mMessageTextView.setText(getArguments().getString(KEY_MESSAGE)); mSubmitButton = view.findViewById(R.id. submitButton); mSubmitButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d("MyFragment", "Somebody click on this button!"); Log.d("MyFragment", "Title: " + mTitleTextView.toString() + ", Message: " + mMessageTextView.toString()); } }); } }

Slide 18

Slide 18 text

JAVA TO KOTLIN… JAVA STYLE public class MyFragment extends Fragment { private static final String KEY_TITLE = "KEY_TITLE"; private static final String KEY_MESSAGE = "KEY_MESSAGE"; public static MyFragment newInstance(String newTitle, String newMessage) { Bundle args = new Bundle(); args.putString(KEY_TITLE, newTitle); args.putString(KEY_MESSAGE, newMessage); MyFragment fragment = new MyFragment(); fragment.setArguments(args); return fragment; } }

Slide 19

Slide 19 text

JAVA TO KOTLIN… JAVA STYLE class MyFragment : Fragment() { companion object { private val KEY_TITLE = "KEY_TITLE" private val KEY_MESSAGE = "KEY_MESSAGE" fun newInstance(newTitle: String, newMessage: String): MyFragment { val args = Bundle() args.putString(KEY_TITLE, newTitle) args.putString(KEY_MESSAGE, newMessage) val fragment = MyFragment() fragment.arguments = args return fragment } } }

Slide 20

Slide 20 text

JAVA TO KOTLIN… JAVA STYLE + private const val KEY_TITLE = "KEY_TITLE" + private const val KEY_MESSAGE = "KEY_MESSAGE" class MyFragment : Fragment() { companion object { - private val KEY_TITLE = "KEY_TITLE" - private val KEY_MESSAGE = "KEY_MESSAGE" fun newInstance(newTitle: String, newMessage: String): MyFragment { val args = Bundle() args.putString(KEY_TITLE, newTitle) args.putString(KEY_MESSAGE, newMessage) val fragment = MyFragment() fragment.arguments = args return fragment } } }

Slide 21

Slide 21 text

JAVA TO KOTLIN… JAVA STYLE private const val KEY_TITLE = "KEY_TITLE" private const val KEY_MESSAGE = "KEY_MESSAGE" class MyFragment : Fragment() { companion object { fun newInstance(newTitle: String, newMessage: String): MyFragment { val args = Bundle() args.putString(KEY_TITLE, newTitle) args.putString(KEY_MESSAGE, newMessage) val fragment = MyFragment() fragment.arguments = args return fragment } } }

Slide 22

Slide 22 text

JAVA TO KOTLIN… JAVA STYLE private const val KEY_TITLE = "KEY_TITLE" private const val KEY_MESSAGE = "KEY_MESSAGE" + private val Bundle.title: String class MyFragment : Fragment() { companion object { fun newInstance(newTitle: String, newMessage: String): MyFragment { val args = Bundle() args.putString(KEY_TITLE, newTitle) args.putString(KEY_MESSAGE, newMessage) val fragment = MyFragment() fragment.arguments = args return fragment } } }

Slide 23

Slide 23 text

JAVA TO KOTLIN… JAVA STYLE private const val KEY_TITLE = "KEY_TITLE" private const val KEY_MESSAGE = "KEY_MESSAGE" + private val Bundle.title: String + get() = getString(KEY_TITLE) class MyFragment : Fragment() { companion object { fun newInstance(newTitle: String, newMessage: String): MyFragment { val args = Bundle() args.putString(KEY_TITLE, newTitle) args.putString(KEY_MESSAGE, newMessage) val fragment = MyFragment() fragment.arguments = args return fragment } } }

Slide 24

Slide 24 text

JAVA TO KOTLIN… JAVA STYLE private const val KEY_TITLE = "KEY_TITLE" private const val KEY_MESSAGE = "KEY_MESSAGE" + private var Bundle.title: String + get() = getString(KEY_TITLE) + set(value) { + putString(KEY_TITLE, value) + } class MyFragment : Fragment() { companion object { fun newInstance(newTitle: String, newMessage: String): MyFragment { val args = Bundle() args.putString(KEY_TITLE, newTitle) args.putString(KEY_MESSAGE, newMessage) val fragment = MyFragment() fragment.arguments = args return fragment } } }

Slide 25

Slide 25 text

JAVA TO KOTLIN… JAVA STYLE private const val KEY_TITLE = "KEY_TITLE" private const val KEY_MESSAGE = "KEY_MESSAGE" private var Bundle.title: String get() = getString(KEY_TITLE) set(value) { putString(KEY_TITLE, value) } + private var Bundle.message: String + get() = getString(KEY_MESSAGE) + set(value) { + putString(KEY_MESSAGE, value) + } class MyFragment : Fragment() { companion object { fun newInstance(newTitle: String, newMessage: String): MyFragment { val args = Bundle() args.putString(KEY_TITLE, newTitle) args.putString(KEY_MESSAGE, newMessage) val fragment = MyFragment() fragment.arguments = args return fragment } } }

Slide 26

Slide 26 text

JAVA TO KOTLIN… JAVA STYLE private const val KEY_TITLE = "KEY_TITLE" private const val KEY_MESSAGE = "KEY_MESSAGE" private var Bundle.title: String get() = getString(KEY_TITLE) set(value) { putString(KEY_TITLE, value) } private var Bundle.message: String get() = getString(KEY_MESSAGE) set(value) { putString(KEY_MESSAGE, value) } class MyFragment : Fragment() { companion object { fun newInstance(newTitle: String, newMessage: String): MyFragment { val args = Bundle() - args.putString(KEY_TITLE, newTitle) - args.putString(KEY_MESSAGE, newMessage) + args.title = newTitle + args.message = newMessage val fragment = MyFragment() fragment.arguments = args return fragment }

Slide 27

Slide 27 text

JAVA TO KOTLIN… JAVA STYLE private const val KEY_TITLE = "KEY_TITLE" private const val KEY_MESSAGE = "KEY_MESSAGE" private var Bundle.title: String get() = getString(KEY_TITLE) set(value) { putString(KEY_TITLE, value) } private var Bundle.message: String get() = getString(KEY_MESSAGE) set(value) { putString(KEY_MESSAGE, value) } class MyFragment : Fragment() { companion object { fun newInstance(newTitle: String, newMessage: String): MyFragment { val args = Bundle() args.title = newTitle args.message = newMessage val fragment = MyFragment() fragment.arguments = args return fragment } } }

Slide 28

Slide 28 text

JAVA TO KOTLIN… JAVA STYLE class MyFragment : Fragment() { companion object { fun newInstance(newTitle: String, newMessage: String): MyFragment { val args = Bundle() args.title = newTitle args.message = newMessage val fragment = MyFragment() fragment.arguments = args return fragment } } }

Slide 29

Slide 29 text

JAVA TO KOTLIN… JAVA STYLE class MyFragment : Fragment() { companion object { fun newInstance(newTitle: String, newMessage: String): MyFragment { - val args = Bundle() - args.title = newTitle - args.message = newMessage + val args = Bundle().apply { + title = newTitle + message = newMessage + } val fragment = MyFragment() fragment.arguments = args return fragment } } }

Slide 30

Slide 30 text

JAVA TO KOTLIN… JAVA STYLE class MyFragment : Fragment() { companion object { fun newInstance(newTitle: String, newMessage: String): MyFragment { val args = Bundle().apply { title = newTitle message = newMessage } val fragment = MyFragment() fragment.arguments = args return fragment } } }

Slide 31

Slide 31 text

JAVA TO KOTLIN… JAVA STYLE class MyFragment : Fragment() { companion object { fun newInstance(newTitle: String, newMessage: String): MyFragment { - val args = Bundle().apply { - title = newTitle - message = newMessage - } - val fragment = MyFragment() - fragment.arguments = args - return fragment + return MyFragment().apply { + arguments = Bundle().apply { + title = newTitle + message = newMessage + } + } } } }

Slide 32

Slide 32 text

JAVA TO KOTLIN… JAVA STYLE class MyFragment : Fragment() { companion object { fun newInstance(newTitle: String, newMessage: String): MyFragment { return MyFragment().apply { arguments = Bundle().apply { title = newTitle message = newMessage } } } } }

Slide 33

Slide 33 text

JAVA TO KOTLIN… JAVA STYLE private const val KEY_TITLE = "KEY_TITLE" private const val KEY_MESSAGE = "KEY_MESSAGE" private var Bundle.title: String get() = getString(KEY_TITLE) set(value) { putString(KEY_TITLE, value) } private var Bundle.message: String get() = getString(KEY_MESSAGE) set(value) { putString(KEY_MESSAGE, value) } class MyFragment : Fragment() { companion object { fun newInstance(newTitle: String, newMessage: String): MyFragment { return MyFragment().apply { arguments = Bundle().apply { title = newTitle message = newMessage } } } } }

Slide 34

Slide 34 text

JAVA TO KOTLIN… JAVA STYLE public class MyFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.my_fragment, container); } }

Slide 35

Slide 35 text

JAVA TO KOTLIN… JAVA STYLE override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { return inflater?.inflate(R.layout.my_fragment, container) ?: LayoutInflater.from(context).inflate(R.layout.my_fragment, container) }

Slide 36

Slide 36 text

JAVA TO KOTLIN… JAVA STYLE

Slide 37

Slide 37 text

JAVA TO KOTLIN… JAVA STYLE public class MyFragment extends Fragment { private TextView mTitleTextView; private TextView mMessageTextView; private Button mSubmitButton; @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); mTitleTextView = view.findViewById(R.id.titleTextView); mTitleTextView.setText(getArguments().getString(KEY_TITLE)); mMessageTextView = view.findViewById(R.id.messageTextView); mMessageTextView.setText(getArguments().getString(KEY_MESSAGE)); mSubmitButton = view.findViewById(R.id. submitButton); mSubmitButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d("MyFragment", "Somebody click on this button!"); Log.d("MyFragment", "Title: " + mTitleTextView.toString() + ", Message: " + mMessageTextView.toString()); } }); } }

Slide 38

Slide 38 text

JAVA TO KOTLIN… JAVA STYLE class MyFragment : Fragment() { private var mTitleTextView: TextView? = null private var mMessageTextView: TextView? = null private var mSubmitButton: Button? = null override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) mTitleTextView = view!!.findViewById(R.id.titleTextView) mTitleTextView!!.text = arguments.getString(KEY_TITLE) mMessageTextView = view.findViewById(R.id.messageTextView) mMessageTextView!!.text = arguments.getString(KEY_MESSAGE) mSubmitButton = view.findViewById(R.id.submitButton) mSubmitButton!!.setOnClickListener { Log.d("MyFragment", "Somebody click on this button!") Log.d("MyFragment", "Title: " + mTitleTextView!!.toString() + ", Message: " + mMessageTextView!!.toString()) } } }

Slide 39

Slide 39 text

JAVA TO KOTLIN… JAVA STYLE class MyFragment : Fragment() { private var mTitleTextView: TextView? = null private var mMessageTextView: TextView? = null private var mSubmitButton: Button? = null override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) mTitleTextView = view!!.findViewById(R.id.titleTextView) - mTitleTextView!!.text = arguments.getString(KEY_TITLE) + mTitleTextView!!.text = arguments.title mMessageTextView = view.findViewById(R.id.messageTextView) - mMessageTextView!!.text = arguments.getString(KEY_MESSAGE) + mMessageTextView!!.text = arguments.message mSubmitButton = view.findViewById(R.id.submitButton) mSubmitButton!!.setOnClickListener { Log.d("MyFragment", "Somebody click on this button!") Log.d("MyFragment", "Title: " + mTitleTextView!!.toString() + ", Message: " + mMessageTextView!!.toString()) } } }

Slide 40

Slide 40 text

JAVA TO KOTLIN… JAVA STYLE class MyFragment : Fragment() { private var mTitleTextView: TextView? = null private var mMessageTextView: TextView? = null private var mSubmitButton: Button? = null override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) mTitleTextView = view!!.findViewById(R.id.titleTextView) mTitleTextView!!.text = arguments.title mMessageTextView = view.findViewById(R.id.messageTextView) mMessageTextView!!.text = arguments.message mSubmitButton = view.findViewById(R.id.submitButton) mSubmitButton!!.setOnClickListener { Log.d("MyFragment", "Somebody click on this button!") Log.d("MyFragment", "Title: " + mTitleTextView!!.toString() + ", Message: " + mMessageTextView!!.toString()) } } }

Slide 41

Slide 41 text

JAVA TO KOTLIN… JAVA STYLE class MyFragment : Fragment() { - private var mTitleTextView: TextView? = null - private var mMessageTextView: TextView? = null - private var mSubmitButton: Button? = null + private lateinit var mTitleTextView: TextView + private lateinit var mMessageTextView: TextView + private lateinit var mSubmitButton: Button override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) mTitleTextView = view!!.findViewById(R.id.titleTextView) mTitleTextView!!.text = arguments.title mMessageTextView = view.findViewById(R.id.messageTextView) mMessageTextView!!.text = arguments.message mSubmitButton = view.findViewById(R.id.submitButton) mSubmitButton!!.setOnClickListener { Log.d("MyFragment", "Somebody click on this button!") Log.d("MyFragment", "Title: " + mTitleTextView!!.toString() + ", Message: " + mMessageTextView!!.toString()) } } }

Slide 42

Slide 42 text

JAVA TO KOTLIN… JAVA STYLE class MyFragment : Fragment() { private lateinit var mTitleTextView: TextView private lateinit var mMessageTextView: TextView private lateinit var mSubmitButton: Button override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) mTitleTextView = view!!.findViewById(R.id.titleTextView) mTitleTextView!!.text = arguments.title mMessageTextView = view.findViewById(R.id.messageTextView) mMessageTextView!!.text = arguments.message mSubmitButton = view.findViewById(R.id.submitButton) mSubmitButton!!.setOnClickListener { Log.d("MyFragment", "Somebody click on this button!") Log.d("MyFragment", "Title: " + mTitleTextView!!.toString() + ", Message: " + mMessageTextView!!.toString()) } } }

Slide 43

Slide 43 text

JAVA TO KOTLIN… JAVA STYLE class MyFragment : Fragment() { private lateinit var mTitleTextView: TextView private lateinit var mMessageTextView: TextView private lateinit var mSubmitButton: Button override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) mTitleTextView = view!!.findViewById(R.id.titleTextView) - mTitleTextView!!.text = arguments.title + mTitleTextView.text = arguments.title mMessageTextView = view.findViewById(R.id.messageTextView) - mMessageTextView!!.text = arguments.message + mMessageTextView.text = arguments.message mSubmitButton = view.findViewById(R.id.submitButton) - mSubmitButton!!.setOnClickListener { + mSubmitButton.setOnClickListener { Log.d("MyFragment", "Somebody click on this button!") Log.d("MyFragment", "Title: " + mTitleTextView!!.toString() + ", Message: " + mMessageTextView!!.toString()) } } }

Slide 44

Slide 44 text

class MyFragment : Fragment() { private lateinit var mTitleTextView: TextView private lateinit var mMessageTextView: TextView private lateinit var mSubmitButton: Button override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) mTitleTextView = view!!.findViewById(R.id.titleTextView) mTitleTextView.text = arguments.title mMessageTextView = view.findViewById(R.id.messageTextView) mMessageTextView.text = arguments.message mSubmitButton = view.findViewById(R.id.submitButton) mSubmitButton.setOnClickListener { Log.d("MyFragment", "Somebody click on this button!") Log.d("MyFragment", "Title: " + mTitleTextView.toString() + ", Message: " + mMessageTextView.toString()) } } } JAVA TO KOTLIN… JAVA STYLE

Slide 45

Slide 45 text

class MyFragment : Fragment() { private lateinit var mTitleTextView: TextView private lateinit var mMessageTextView: TextView private lateinit var mSubmitButton: Button override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) mTitleTextView = view!!.findViewById(R.id.titleTextView) mTitleTextView.text = arguments.title mMessageTextView = view.findViewById(R.id.messageTextView) mMessageTextView.text = arguments.message mSubmitButton = view.findViewById(R.id.submitButton) mSubmitButton.setOnClickListener { Log.d("MyFragment", "Somebody click on this button!") - Log.d("MyFragment", "Title: " + mTitleTextView.toString() - + ", Message: " + mMessageTextView.toString()) + Log.d("MyFragment", "Title: $mTitleTextView, Message: $mMessageTextView") } } } JAVA TO KOTLIN… JAVA STYLE

Slide 46

Slide 46 text

class MyFragment : Fragment() { private lateinit var mTitleTextView: TextView private lateinit var mMessageTextView: TextView private lateinit var mSubmitButton: Button override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) mTitleTextView = view!!.findViewById(R.id.titleTextView) mTitleTextView.text = arguments.title mMessageTextView = view.findViewById(R.id.messageTextView) mMessageTextView.text = arguments.message mSubmitButton = view.findViewById(R.id.submitButton) mSubmitButton.setOnClickListener { Log.d("MyFragment", "Somebody click on this button!") Log.d("MyFragment", "Title: $mTitleTextView, Message: $mMessageTextView") } } } JAVA TO KOTLIN… JAVA STYLE

Slide 47

Slide 47 text

// Top level build.gradle file buildscript { ext.kotlin_version = '1.2.30' // ... dependencies { // ... classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } // build.gradle file of your app module // ... apply plugin: ‘kotlin-android' + apply plugin: 'kotlin-android-extensions' // ... dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" // ... } JAVA TO KOTLIN… JAVA STYLE

Slide 48

Slide 48 text

class MyFragment : Fragment() { private lateinit var mTitleTextView: TextView private lateinit var mMessageTextView: TextView private lateinit var mSubmitButton: Button override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) mTitleTextView = view!!.findViewById(R.id.titleTextView) mTitleTextView.text = arguments.title mMessageTextView = view.findViewById(R.id.messageTextView) mMessageTextView.text = arguments.message mSubmitButton = view.findViewById(R.id.submitButton) mSubmitButton.setOnClickListener { Log.d("MyFragment", "Somebody click on this button!") Log.d("MyFragment", "Title: $mTitleTextView, Message: $mMessageTextView") } } } JAVA TO KOTLIN… JAVA STYLE

Slide 49

Slide 49 text

+ import kotlinx.android.synthetic.main.my_fragment.* class MyFragment : Fragment() { - private lateinit var mTitleTextView: TextView - private lateinit var mMessageTextView: TextView - private lateinit var mSubmitButton: Button override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - mTitleTextView = view!!.findViewById(R.id.titleTextView) mTitleTextView.text = arguments.title - mMessageTextView = view.findViewById(R.id.messageTextView) mMessageTextView.text = arguments.message - mSubmitButton = view.findViewById(R.id.submitButton) mSubmitButton.setOnClickListener { Log.d("MyFragment", "Somebody click on this button!") Log.d("MyFragment", "Title: $mTitleTextView, Message: $mMessageTextView") } } } JAVA TO KOTLIN… JAVA STYLE

Slide 50

Slide 50 text

import kotlinx.android.synthetic.main.my_fragment.* class MyFragment : Fragment() { override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) titleTextView.text = arguments.title messageTextView.text = arguments.message submitButton.setOnClickListener { Log.d("MyFragment", "Somebody click on this button!") Log.d("MyFragment", "Title: $mTitleTextView, Message: $mMessageTextView") } } } JAVA TO KOTLIN… JAVA STYLE

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

KOTLIN TO BINARY TO JAVA

Slide 53

Slide 53 text

KOTLIN TO BINARY TO JAVA

Slide 54

Slide 54 text

class MyClass abstract class MyAbsClass open class MyOpenClass data class MyDataClass(val field: String) KOTLIN TO BINARY TO JAVA

Slide 55

Slide 55 text

KOTLIN TO BINARY TO JAVA

Slide 56

Slide 56 text

public final class MyClass { } public abstract class MyAbsClass { } public class MyOpenClass { } KOTLIN TO BINARY TO JAVA

Slide 57

Slide 57 text

public final class MyDataClass { @NotNull private final String field; @NotNull public final String getField() { return this.field; } public MyDataClass(@NotNull String field) { // ... } @NotNull public final MyDataClass copy(@NotNull String field) { // ... } public String toString() { // ... } public int hashCode() { // ... } public boolean equals(Object var1) { // ... } } KOTLIN TO BINARY TO JAVA

Slide 58

Slide 58 text

open class MethodExamples { fun m() {} open fun mOpen() {} } interface Method2Examples { fun mOpen() {} } class Usage : MethodExamples(), Method2Examples{ override fun mOpen() { super.mOpen() super.mOpen() } } KOTLIN TO BINARY TO JAVA

Slide 59

Slide 59 text

public class MethodExamples { public final void m() { } public void mOpen() { } } public interface Method2Examples { void mOpen(); public static final class DefaultImpls { public static void mOpen(Method2Examples $this) { } } } public final class Usage extends MethodExamples implements Method2Examples { public void mOpen() { Method2Examples.DefaultImpls.mOpen(this); super.mOpen(); } } KOTLIN TO BINARY TO JAVA

Slide 60

Slide 60 text

class MyClass(val listener: (test: String) -> Unit) { fun m() { listener("Result") } } fun main(args: Array) { MyClass { it -> // Do something really cool! } } KOTLIN TO BINARY TO JAVA

Slide 61

Slide 61 text

public final class MyClass { @NotNull private final Function1 listener; public final void m() { this.listener.invoke("Result"); } @NotNull public final Function1 getListener() { return this.listener; } public MyClass(@NotNull Function1 listener) { Intrinsics.checkParameterIsNotNull(listener, "listener"); super(); this.listener = listener; } } KOTLIN TO BINARY TO JAVA

Slide 62

Slide 62 text

/** A function that takes 0 arguments. */ public interface Function0 : Function { /** Invokes the function. */ public operator fun invoke(): R } /** A function that takes 1 argument. */ public interface Function1 : Function { /** Invokes the function with the specified argument. */ public operator fun invoke(p1: P1): R } /** A function that takes 2 arguments. */ public interface Function2 : Function { /** Invokes the function with the specified arguments. */ public operator fun invoke(p1: P1, p2: P2): R } // ... /** A function that takes 22 arguments. */ public interface Function22 : Function { /** Invokes the function with the specified arguments. */ public operator fun invoke(p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6, p7: P7, p8: P8, p9: P9 p10: P10, p11: P11, p12: P12, p13: P13, p14: P14, p15: P15, p16: P16, p17: P17, p18: P18, p19: P19, p P20, p21: P21, p22: P22): R } KOTLIN TO BINARY TO JAVA

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

fun MutableList.swap(index1: Int, index2: Int) { val tmp = this[index1] this[index1] = this[index2] this[index2] = tmp } KOTLIN TO BINARY TO JAVA

Slide 65

Slide 65 text

public final class ExtensionsKt { public static final void swap(@NotNull List $receiver, int index1, int index2) { Intrinsics.checkParameterIsNotNull($receiver, "$receiver"); int tmp = ((Number)$receiver.get(index1)).intValue(); $receiver.set(index1, $receiver.get(index2)); $receiver.set(index2, Integer.valueOf(tmp)); } } KOTLIN TO BINARY TO JAVA

Slide 66

Slide 66 text

object MyObject { var test: String = "" } fun main(args: Array) { MyObject.test = "new value" } KOTLIN TO BINARY TO JAVA

Slide 67

Slide 67 text

public final class MyObject { @NotNull private static String test; public static final MyObject INSTANCE; @NotNull public final String getTest() { return test; } public final void setTest(@NotNull String var1) { Intrinsics.checkParameterIsNotNull(var1, ""); test = var1; } static { MyObject var0 = new MyObject(); INSTANCE = var0; test = ""; } } KOTLIN TO BINARY TO JAVA

Slide 68

Slide 68 text

TRAPS

Slide 69

Slide 69 text

fun m(typeClass: AbsType) { } open class AbsType class SubType1 : AbsType() class SubType2 : AbsType() TRAPS

Slide 70

Slide 70 text

fun m(typeClass: AbsType) { + when (typeClass) { + is SubType1 -> println("Typed by subtype 1") + is SubType2 -> println("Typed by subtype 2") + else -> println("No type identified") + } } open class AbsType class SubType1 : AbsType() class SubType2 : AbsType() TRAPS

Slide 71

Slide 71 text

fun m(typeClass: AbsType) { when (typeClass) { is SubType1 -> println("Typed by subtype 1") - is SubType2 -> println("Typed by subtype 2") + is SubType2 -> { + println("Typed by subtype 2") + typeClass.children.forEach { + when (it) { + is SubType1 -> println("Child typed by subtype 1") + is SubType2 -> println("Child typed by subtype 2") + else -> println("No type identified") + } + } + } else -> println("No type identified") } } open class AbsType class SubType1 : AbsType() - class SubType2 : AbsType() + class SubType2(val children: List) : AbsType() TRAPS

Slide 72

Slide 72 text

fun m(typeClass: AbsType) { when (typeClass) { is SubType1 -> println("Typed by subtype 1") is SubType2 -> { println("Typed by subtype 2") typeClass.children.forEach { when (it) { is SubType1 -> println("Child typed by subtype 1") is SubType2 -> println("Child typed by subtype 2") else -> println("No type identified") } } } else -> println("No type identified") } } open class AbsType class SubType1 : AbsType() class SubType2(val children: List) : AbsType() TRAPS

Slide 73

Slide 73 text

variable.toList() variable.toInt() variable.joinToString { ... } variable.copy() variable.plus(...) variable.minus(...) ... variable.doEverything() TRAPS

Slide 74

Slide 74 text

WTF (˽°□°҂˽Ɨ ˍʓˍ

Slide 75

Slide 75 text

someObject?.takeIf { status }.apply { doThis() } WTF (˽°□°҂˽Ɨ ˍʓˍ

Slide 76

Slide 76 text

MyNullSafetyClass var7 = status ? someObject : null; doThis(); WTF (˽°□°҂˽Ɨ ˍʓˍ

Slide 77

Slide 77 text

- someObject?.takeIf { status }.apply { doThis() } + someObject?.takeIf { status }?.apply { doThis() } WTF (˽°□°҂˽Ɨ ˍʓˍ

Slide 78

Slide 78 text

MyNullSafetyClass var7 = status ? someObject : null; if((status ? someObject : null) != null) { doThis(); } WTF (˽°□°҂˽Ɨ ˍʓˍ

Slide 79

Slide 79 text

null.apply { ... } WTF (˽°□°҂˽Ɨ ˍʓˍ

Slide 80

Slide 80 text

class MyClass class MyClass2(val test: String, val test2: String) class MyClass3(val clazz: MyClass2?) fun main(args: Array) { val m: MyClass? = MyClass() // No compilation error. if (m != null) { m.toString() } // No compilation error. "${m!!.toString()}${m.toString()}" // No compilation error. val myClass3 = MyClass3(MyClass2("", "")) val test = myClass3.clazz!!.test val test2 = myClass3.clazz.test2 } WTF (˽°□°҂˽Ɨ ˍʓˍ

Slide 81

Slide 81 text

class MyClass class MyClass2(val test: String, val test2: String) class MyClass3(val clazz: MyClass2?) class MyClass4 { private var myClass2: MyClass2? = null fun m() { val test = myClass2!!.test // error, wtf this shit?! val test2 = myClass2.test2 } } WTF (˽°□°҂˽Ɨ ˍʓˍ

Slide 82

Slide 82 text

class MyClass class MyClass2(val test: String, val test2: String) class MyClass3(val clazz: MyClass2?) class MyClass4 { private var myClass2: MyClass2? = null fun m() { if (myClass2 != null) { val test = myClass2!!.test val test2 = myClass2!!.test2 } } } WTF (˽°□°҂˽Ɨ ˍʓˍ

Slide 83

Slide 83 text

SUMMARY - Kotlin is mature and ready for production - Designed to be compatible and better than Java - Easy to learn and high adoption by the Android community - Be curious

Slide 84

Slide 84 text

FROM JAVA TO Gerard Paligot