Development/Android
Kotlin Higher-Order Functions
Lonepine
2019. 5. 21. 11:34
Kotlin Higher-Order Functions
interface를 통해 간단한 연동이 필요할 경우
interface ExampleInterface
{
fun checkFun()
}
fun testFunction(example:ExampleInterface)
{
example.checkFun()
}
와 같이 사용 되던 부분을
fun testFunction(checkFun:()->Unit)
{
checkFun()
}
로 표현 할 수 있다.
비동기 처리와 같이 데이터 처리 후 결과를 리턴 할 경우 Higher-Oder Functions을 사용해
기존의 Interface를 사용 하지 않고 코드량을 줄여서 사용 할 수 있다.
---------- 설명 ---------
fun testFunction(checkFun:()->Unit)
checkFun => 함수명
() => 리턴값이 없을 경우 () 로 표현한다.
데이터를 넘겨야 할 경우 데이터 타입을 선언하면된다.
(String) ->. Unit 과 같은 형태
(String,Int) -> Unit 등..
Unit => 리턴값은 없기 때문에 Unit으로 처리