PK - Chapter 4 Flashcards
# *_Define a function_* "foo" with an Int parameter and an Int return of the parameter squared.
fun foo(pInt: Int) = pInt * pInt
- the Int return value is inferred by the compiler
- can only be inferred for a single expression
# Define a function "foo" with a String parameter and a String return of the parameter contatenated with "Hello World".
fun foo(pString: String) = “${pString} and Hello World”
What is a single expression function ?
- Only has a single expression
- does not need braces
- does not need to specify the return type
- uses the “=” sign rather than the “return” keyword
Describe member functions.
- defined inside a class, object or interface
- invoked using the name of the containing class,
or object, with a dot notation - member functions can reference other functions
in the object or class, with no additional syntax - member functions can access and modify member
variables of the class
What is the DRY principle ?
Don’t Repeat Yourself
Describe Local Functions.
- also called nested functions
- declared inside other functions
- can be nested several times
- not accessible to code outside the containing function
- can access parameters and variables in the outer scope
- does not need to define the parameters or variables in the outer scope
Use local functions inside a for loop
and when clause.
fun fizzBuzz(start: Int, end: Int) { for (k in start..end) { fun isFizz() = k % 3 == 0 fun isBuzz() = k % 5 == 0 when ( isFizz() && isBuzz() -\> println("Fizz Buzz") isFizz() -\> println("Fizz") isBuzz() -\> println("Buzz") } } }
Explain top-level functions.
- defined outside any class, object or interface
- used to create standalone functions that do
not belong to any object
What are the 4 types of functions ?
- member functions
- extension functions (special case of member function)
- local functions
- top-level functions
Explain named parameters.
- when calling a function, the parameters can be named
- explicit naming makes the intent clear
- minimizes errors
- not all parameters need to be names
- once a parameter has been named, all subsequent
parameters in the signature must be named - named parameters allow the order of parameters to
be changed - cannot be used on Java-defined functions
Explain default parameters.
- can be used to define functions and constructors
- specified in the signature with the “=” sign
- can omit parameters with a default
- once a parameter is omitted, all subsequent
parameters must be omitted, unless the
parameters are named - default parameters, used with named parameters,
is very powerful and eliminates bolierplate code
for overrides - when overriding a function with default parameters,
we must keep the same function signature
Explain extension functions.
- enables you to create a new method on a class,
without extending or wrapping it - you cannot override a function in the underlying class
that has the same signature (name, parameters,
order, return) - when defined as a top-level function in another file,
it must be imported
Why create an extension function ?
* frequently used to add a function to a final class, or a class in an SDK that you do not want to extend * avoids creating subclasses, or wrappers, which would require code changes to use the new class
# Define extension function "bar{}" for class "Foo".
fun Foo.bar() = “Foo Bar !”
Explain extension function precedence.
Extension function are resolved in the following order:
- look for the method signature in the current receiver
- look in the superclass(es)
- look at the extension imports that are in scope
Create an extension function that handles nulls.
fun Any?.safeEquals(other: Any?): Boolean {
if (this == null && other == null) return true
if (this == null return false
return this.equals(other)
}
What is a member extension function ?
Extension functions are usually declared at the top level.
A member extension function is defined inside a class.
Its scope is limited and no import is required.
Give an example of a mapping receiver
versus an extension receiver.
Extension receiver is an object of the class on which the extension was defined and called.
A method in a class is called the dispatch receiver.
When there is shadowing of names, the extension receiver
will take precedence, so you must qualify the dispatch receiver.
private fun String.stringAdd() {
map.put(this@ContainerClass.hashCode(), this)
}
Create a companion object extension.
fun Int.Companion.random() : Int {
val random = Random()
return random.nextInt()
}
val int = Int.random()
Explain the types; Pair and Triple.
These contain 2 and 3 identical types, respectively.
An example of Pair…
fun getPair() = Pair(getSomeInt(), getAnotherInt())
val (firstInt, secondInt) = getPair()
val myPair = “London” to “Paris”
Explain an infix function .
- a function that is placed between 2 operands or arguments,
so can only operate on 2 arguments - first argument is the instance that the function
is invoked on - the second argument is an explicit parameter
to the function
infix fun concat(other: String) = this + other
val myConcat = “Hello “ concat “World!”
Explain operator overloading .
- the ability to define functions that use operators
- operators can only be defined as member functions
or extension functions - can also be referenced with the dot notation and
english label - can act on a different class than they are defined in
- there is a fixed list of operators that can be used as
functions, each with an English equivalent name
What is the list of basic operators
that can be overloaded ?
plus
minus
times
div
mod
rangeTo
unaryPlus
unaryMinus
not