3
我想將函數傳遞給Kotlin中的函數,這裏是我的代碼。Kotlin:獲取一個類的函數的參考'實例
fun validateValueWithFunc(value: String, parsefun: (CharSequence) -> Boolean, type: String){
if(parsefun(value))
print("Valid ${type}")
else
print("Invalid ${type}")
}
我傳遞的功能是從正則表達式類「containsMatchIn」
val f = Regex.fromLiteral("some regex").containsMatchIn
我瞭解::函數引用運營商,但我不知道如何在這種情況下
使用
也許會更清楚地說明您正在使用lamda「捕獲」函數來捕獲實例,直到綁定的可調用引用可用。你的語法的第一個版本對初學者來說很難說服。 –
@JaysonMinard,更新了答案。 – hotkey
值得注意的是,'regex.containsMatchIn(s)'也可以表示爲's中的正則表達式' – Ilya