함수형 인터페이스에 대해 함수 탕비을 사용해 행동을 표현할 때가 자주 있음
fun interface를 사용해 함수형 인터페이스를 정의할 수 있음
fun interface IntCondition {
fun check(I: Int) : Boolean // 추상 메서드
fun checkString(s: String) = check(s.toInt()) // 비추상 메서드
fun checkChar(c: Char) = check(c.digitToInt()) // 비추상 메서드
}
fun main() {
val isOdd = IntCondition { it % 2 != 0 }
println(isOdd.check(1))
// true
println(isOdd.checkString("2"))
// false
println(isOdd.checkChar('3'))
// true
}
fun interface IntCondition {
fun check(I: Int) : Boolean // 추상 메서드
fun checkString(s: String) = check(s.toInt()) // 비추상 메서드
fun checkChar(c: Char) = check(c.digitToInt()) // 비추상 메서드
}
fun checkCondition(i: Int, condition: IntCondition): Boolean {
return condition.check(i)
}
fun main() {
println(checkCondition(1) { it % 2 != 0 }) //람다를 직접 사용
// true
val isOdd: (Int) -> Boolean = { it % 2 != 0 } // 시그니처가 일치하는 람다에 대한 참조식 사용
println(checkCondition(1, isOdd))
// true
}