Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction à Kotlin

Introduction à Kotlin

Introduction à Kotlin, un nouveau langage pour Android et la JVM.

Présenté dans le cadre du Meetup Mobile Grenoble.

Avatar for Clément Plantier

Clément Plantier

November 15, 2016
Tweet

Other Decks in Programming

Transcript

  1. Java comme langage officiel Java, avec un peu de retard

    sur le support de la dernière version Initialement : Java 6 Java 7 (2011) supporté 2014 Java 8 (2014) supporté en 2016 2
  2. Kotlin Projet initié par JetBrains en 2010 (version 1.0 fin

    2015) Langage objet et fonctionnel 100% interopérable avec Java Compile aussi vers du JS 6
  3. Syntaxe 7 fun sum(a: Int, b: Int): Int { return

    a + b } fun sum(a: Int, b: Int) = a + b
  4. 8 Syntaxe public class User { private String firstName; private

    String lastName; public User(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } } En Java...
  5. 10 Data class data class User(var firstName: String?, var lastName:

    String?) .equals(...) .hashCode() .toString() .copy(firstName = “Donald”)
  6. Langage fonctionnel val admins = users.filter { user -> user.isAdmin

    } val admins = users.filter { it.isAdmin } val nbWords = tweets .map { it.split(" ").size } .reduce { x, y -> x + y } etc... 11
  7. Fonctions d’extension Fini les classes “Util”... fun String.stripAccents(): String {

    val normalized = Normalizer.normalize(this, Normalizer.Form.NFD) return normalized.replace("[\\p{InCombiningDiacriticalMarks}]", "") } val sortedUsers = users.sortedBy { it.firstName.toLowerCase().stripAccents() } ...tout en restant compatible avec Java : StringUtils.stripAccents(user.getFirstName()) 12
  8. Sucre syntaxique String interpolation println("User is $user") 13 fun printCity(city:

    String?) { println(city ?: "unknown") } Optionnels var profile: Profile? = null fun printProfileCity() { println(profile?.location?.city) }
  9. Property delegates Lazy props class User(val firstName: String, val lastName:

    String) { val fullName by lazy { "$firstName $lastName" } } 14 Préférences class GlobalPreferences(preferences: SharedPreferences) : KoreferenceModel(preferences) { var tutorialShown by booleanPreference() }
  10. Android Extensions class MyActivity : Activity() { override fun onCreate(savedInstanceState:

    Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) textView.setText("Hello, world!") // (findViewById(R.id.textView) as TextView).setText("Hello, world!") } } 15
  11. Pourquoi Kotlin 100% interopérable avec Java : migration en douceur,

    progressive Très simple à apprendre, accessible à tous Moins de boilerplate (data classes, extensions) Typage fort (null-safety) Fonctionnel 16
  12. Ressources Site officiel Kotlin https://kotlinlang.org Android Development with Kotlin -

    Jake Wharton https://www.youtube.com/watch?v=A2LukgT2mKc Rencontre avec Gaetan Zoritchak, Kotlin addict http://www.duchess-france.org/rencontre-gaetan-zoritchak-kotlin-addict 17