Skip to content

Instantly share code, notes, and snippets.

@gbzarelli
Last active October 9, 2018 14:10
Show Gist options
  • Save gbzarelli/92ff30a41904ddbb425a8ccc2cd2c02f to your computer and use it in GitHub Desktop.
Save gbzarelli/92ff30a41904ddbb425a8ccc2cd2c02f to your computer and use it in GitHub Desktop.
Sample Kotlin Functional Interface
/**
* A interface funcional é declarada no método:
* myFuncionalInterface: (Int,String) -> Unit
* A interface acima tem como parametro um Int e uma String, ele é chamado como se fosse um método pelo proprio nome:
* myFuncionalInterface(1,"Sucesso")
*/
fun methodFuncionalInterface(username: String, password: String, myFuncionalInterface: (Int,String) -> Unit) {
...
myFuncionalInterface(1,"Sucesso")
...
}
/**
* A interface funcional é passada no método que a recebe da seguinte maneira:
* {paramInt, paramString ->
* print(paramInt)
* print(paramString)
* }
* (Sua sintaxe é igual ao lambda.)
*/
fun teste(){
methodFuncionalInterface("username","pass",
{paramInt, paramString ->
print(paramInt)
print(paramString)
}
)
}
//Refêrencia: https://medium.com/google-developers/from-functional-java-to-functioning-kotlin-a4874a4a7a5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment