August 2018
Beginner
178 pages
4h 56m
English
If you already have a member function that you'd like to assign to a function type, you could wrap it inside a lambda:
var numFunc: (Int, Int) -> Int = { a, b -> multiplier(a, b) }fun multiplier(a: Int, b: Int): Int { return a * b}
You can also initialize the function type directly with a member reference, the same as you can in Java 8:
var numFunc: (Int, Int) -> Int = this::multiplierfun multiplier(a: Int, b: Int): Int { return a * b}
Member references are expressed with the :: operator. On the left-hand side of the operator is the object that holds the function. We use this since we are referring to the same object that holds the numFunc function type property. On the right-hand side is the function name.
When the function ...
Read now
Unlock full access