val isUnit = println("This is an expression")
println(isUnit)
println 이라는 아이는 값이 아닌 행위인 아이지 , 이를 찍으면 kotlin.Unit
이 나온다.
Kotlin's Unit is equivalent to Java's void.
we'll write some code to assign a println() to a variable called isUnit, and then print it. Since println() does not return a value, it returns kotlin.Unit.!
fun printHello(name: String?): Unit {
println("Hi there!")
}
fun drive(speed: String = "fast") {
println("driving $speed")
}
fun ggaeSongPyeon(args: String="깨송편") {
print('\n'+args)
}
ggaeSongPyeon() # 아무것도 안 넣으면 기본 인자인 깨송편
ggaeSongPyeon("콩송편")
fun reformat(str: String,
divideByCamelHumps: Boolean,
wordSeparator: Char,
normalizeCase: Boolean = true)
reformat(str, divideByCamelHumps = false, wordSeparator = '_')
function type 을 쓰는 syntax 는 “(Parameter types) -> Return type” 이다.
waterFilter can be any function that takes an Int and returns an Int.
Fuction type with a receiver type : A.(B) -> C
encodeMessage("acronym", { input -> input.toUpperCase() })
fun encodeMsg(msg: String, encode: (String) -> String): String {
return encode(msg)
}
Scope Functions 함수들을 람다식을 이용해서 호출하면 일시적인 Scope(범위)가 생김
이 범위 안에서는 전달된 객체에 대해 "it" 또는 "this" 라는 Context Object를 통해서 접근
Scope Function에는 서로 다른 두 가지 주요 차이점
class Person (var name: String, var age: Int)
fun main() {
val person = Person("홍길동", 30)
//this로 참조
person.run {
println("이름 : ${name}") //this.name과 동일
}
//it로 참조
person.let {
println("이름 : ${it.name}")
}
}
- run, with, apply 는 Context Object를 "this" 로 참조
- 따라서, 람다식 안에서는 일반 클래스 멤버처럼 사용할 수 있습니다.
- this는 생략할 수 있지만, 만약 동일한 이름의 멤버가 있을 경우 구별할 수가 없기 때문에, 가급적이면 Context Object에 대해서는 this를 붙여서 사용
class Person (var name: String, var age: Int)
fun main() {
val person = Person("홍길동", 30)
//this로 참조
person.run {
println("이름 : ${this.name}")
}
}
class Person (var name: String, var age: Int)
fun main() {
val person = Person("홍길동", 30)
//it로 참조
person.let {
println("이름 : ${it.name}")
}
//전달 인자명 지정해서 참조
person.let { value ->
println("이름 : ${value.name}")
}
}
https://docs.google.com/presentation/u/0/
https://jjeda.tistory.com/21
https://aroundck.tistory.com/4869
https://0391kjy.tistory.com/25
https://medium.com/@la.place/higher-order-function-%EC%9D%B4%EB%9E%80-%EB%AC%B4%EC%97%87%EC%9D%B8%EA%B0%80-1c61e0bea79