日記
変化は痛みを伴う*1
今日書いたコード
fun main() { executeFuncWithOneParameter { println("one") } // one executeFuncWithOneParameter { _ -> println("another") } // another // executeFuncWithTwoParameters { print("two") } // Type mismatch: inferred type is () -> Unit but (Int, Int) -> Unit was expected // executeFuncWithTwoParameters { _ -> print("two") } // Type mismatch: inferred type is (Int) -> Unit but (Int, Int) -> Unit was expected executeFuncWithTwoParameters { _, _ -> println("two") } // two } fun executeFuncWithOneParameter(f: (Int) -> Unit) { f(1) } fun executeFuncWithTwoParameters(f: (Int, Int) -> Unit) { f(1, 2) }
感想
Kotlinのlambdaは引数が1個のときは省略できるが2個以上のときはできない.
別の話で,1つのabstract methodのみをプロパティとして持つ(?)オブジェクトを引数として渡すときは,代わりにlambdaを渡すとSAM (Single-Abstract-Method) がいい感じに変換してくれるらしい.
*1:正月休みが終わっただけ