KotlinLearningNote

=Band= Refer to IELTS scoring click
 * band 1-3 need help and or help on finding the document on usage to complete the task
 * band 1 - can hardly form a block of statement - need help
 * band 2 - use extremely limit basic range of language feature - need help
 * band 3 - use very limit basic range of language feature - need help
 * band 4-6 need to read the document or search the community on usage to complete the task
 * band 4 - use basic language feature with misuse - frequently search doc
 * band 5 - use basic language feature but minimally adequate with misuse - frequently search doc
 * band 6 - use adequate range of language feature - no misuse - search doc some times
 * band 7-9 no need document to mentor others on usage to complete the task
 * band 7 - use sufficient range of language feature without searching doc
 * band 8 - use wide range of language feature without searching doc
 * can apply multiple ways of code writing
 * can point to doc to convince others
 * band 9 - use wide range of language feature very naturally without searching doc
 * can explain pros and cons of one way vs the other
 * can explain in his/her own languages with examples to convince others
 * band 10 master of Kotlin

=Language=
 * Language Reference click
 * Kotlin and Swift 2011 vs 2013 swift is like kotlin

Keywords
See kotlin-lang click See kotlin-lang visibility click
 * !! assert non-null
 * .. create a range
 * 1..3 = 1, 2, 3
 * step 2 increment by 2
 * until
 * 1 until 4 = 1, 2, 3
 * downTo
 * for (x in 10 downTo 1) = 10, 9, ... 1
 * :: create a member reference or a class reference
 * ?. perform a safe-call
 * ?: Elvis operator
 * -> separate parameter and body of lambda/function-type/expression
 * _ unused parameter
 * internal visible to within the same module

Basic

 * fun - declares a function.
 * val - declares read-only value object val and immutable is preferred
 * var - declares mutable variable object
 * Prefix a variable name with $ or surround an expression with ${ } to have its value injected into the string.
 * if is now an expression with a return value.
 * when expression is analogous to switch in Java but is more powerful.
 * You don’t have to cast a variable explicitly after checking that it has a certain type: the compiler casts it for you automatically using a smart cast.

Refer kotlin lang idioms click
 * Unit return type means does not return anything
 * lateinit tells compiler aware and to be handled later - using var
 * by lazy val pStr:String by lazy { ...only return once for the string }

Generics
Kotlin generics see click

contravariant

 * in
 * out
 * where

Lambda

 * {x:Int -> x + 5} - given one Int parameter named x, add to 5 then return as result.
 * (Int, Int) -> Int = { a: Int, b: Int -> a + b }
 * val kgsToPounds = { x: Double -> x * 2.204623 }
 * val poundsToUsTons = { x: Double -> x / 2000.0 }

Loop

 * when(intValue) { 0 -> ...
 * 1, 2 -> ..
 * else -> {...}
 * when(pet) { is Wolf -> ..
 * is Hippo -> ...

Property

 * customer setter - set(value){ if (value > 0 ) field = value }
 * customer setter - get = anotherWeight / 2
 * super property
 * open val food = "unknown"
 * overwrite val food = "grass"

Collections

 * arrayOf
 * arrays have functions to copy the array to a new collection: toList, to
 * listOf
 * val friendList = listOf("Jim", "Sue", "Sue", "Nick")
 * val shoppingList = mutableListOf("Tea", "Eggs", "Milk")
 * setOf
 * val friendSet = setOf("Jim", "Sue", "Nick")
 * val friendSet = mutableSetOf("Jim", "Sue")
 * mapOf
 * val recipeMap = mutableMapOf("Recipe1" to r1, "Recipe2" to r2)
 * for ((key, value) in recipeMap) { ...

Classes

 * Interface and abstract class
 * is-a
 * can-do
 * sealed
 * sealed class of sub classes as enum
 * what else?

Constructor

 * constructor
 * init - primary constructor, initialiser.
 * require - a method which throws IllegalArgumentException if on false.

Data Classes

 * data
 * == checks equivalence
 * === checks for object identity

Enum Classes

 * enum

Singleton

 * object Resource { val name = "Name" }

Abstract Class

 * val myInstance = object:SomeAbstractClass { ... overwrite/implement some methods 

Polymorphism

 * It decouples the "what" from "how" at type level.
 * At runtime when objects of a derived class are treated as objects of the base class.

=Essentials=

Statement or Expression

 * "a statement is a syntactic unit of an imperative programming language that expresses some action to be carried out." wikipedia
 * "an expression is a syntactic entity in a programming language that may be evaluated to determine its value." wikipedia

Basic Types
Refer to basic types click

Mutable and Immutable

 * val with Immutable
 * var - who creates, modifies it - risk

Null Safety
Refer to kotlin lang click
 * Elvis operator

Control Flow

 * If else
 * When else
 * Null cast else

Collections
Refer to kotlinlang collection-operations click last filter
 * Transformation
 * Filtering
 * Ordering
 * Grouping
 * Aggregation
 * Plus and minus
 * Retrieving

Sequence
Without ".asSequence" in above the values from the whole list are to be calculated through the pipeline. Refer to kotlinlang sequences click
 * yield

Sequence must be triggered by .first or .toList

Refer to kotlin.collections.first click

Inline Function

 * Compactness
 * Named parameters
 * Default value

Lambda

 * Declare a lambda


 * A lambda without parameter


 * see filter


 * some complicated lambda

Anonymous Function
Or above anonymous function can be written in below in short
 * Declare an anonymous function


 * A rewrite of the complicated "getAgeLamb" can be

Inner Function

 * Local function
 * a function inside a function

Primary constructor
Refer to kotlin lang constructors click

Property from Constructor
Declaring properties and initialising them from the primary constructor as below.

Secondary constructor
Refer to kotlin lang secondary-constructors click

Function Interface
Refer to kotlin lang click
 * SAM

=Kotlin for Android=

Coroutine

 * light weight thread
 * a library outside kotlin core
 * execute in snyc

Continue reading at Kotlin Coroutine Learning Note

Scope Functions
Refer to scope functions let run with apply also
 * also - executes side-effects on the given object
 * apply - configures the given object
 * let - transforms a nullable into another value
 * run - transforms its receiver into another value
 * with - avoid writing the same receiver
 * repeat - repeat for given count of times
 * takeIf - return if predicates true
 * takeUnless - return if predicates false


 * Refer to kotlin expertise click
 * Refer to medium click

apply
Issue with previous syntax Solution with Kotlin

also
=Books and Links=
 * Kotlin in Action
 * Programming Kotlin
 * https://kotlinlang.org/docs/reference/
 * https://play.kotlinlang.org/
 * https://play.kotlinlang.org/koans
 * https://blog.jetbrains.com/kotlin/