Android Kotlin
unique basics
everything has a value
only expressions, no statements
when syntax
acts as switch
i.e
val result = when(money) {
100 -> 'broke'
200 -> 'nice'
else -> 'idk'
}
functions
default args
i.e
fun(money: Int = 100) { ... }
can also be a function
compact functions
single-expression functions
i.e
fun addByTwo(num: Int) = num + 2
fun isLoggedIn(username: String) = username.length > 0
good use cases
paired with when syntax
complex logics
simple value manipuilation
paired with functional programming functions syntax
listOf syntax
acts as an array
i.e
val myArray = listOf(1, 2, 3)
functional programming functions
filter
method of a list
does selection of elimination of a list
usage
concept
if it returns true, then it does not get eliminated
i.e
myArray.filter {
it <= 20
}
lamdas
similar to javascript arrow functions
characteristics
a variable holds an anonymous function
function that is an expression
can be passed as data
i.e
val myLambda: (Int) -> Int = {someInteger -> someInter - 20}
classes & objects
constructor
primary
init
secondary
defined in parameters
auto-assign attributes
add var/val to the parameter name
if you need some logic to constructor
you can have multiple inits
a way for constructor overloading
attributes
getter & setter
automatically defined
custom
if you need extra logic on fetching / setting
i.e
var name: String
get() = name + "his majesty"
set(newName) {newName +"lol")
refers to another attribute