Chapter 6 questions

“You can assign this function to a variable using the method reference operator, ::, like so:

var function = ::add

Here, the name of the variable is function and its type is inferred as (Int, Int) -> Int from the add function you assigned to it. The function variable is of a function type that takes two Int parameters and returns an Int.”

Excerpt From: By Joe Howard. “Kotlin Apprentice.” Apple Books.

(Int, Int) -> Int looks like Swift to me. Could we get an explanation of why -> is used in Kotlin when passing a function as a parameter, but : is used for the return type of a function; whereas in Swift -> is used throughout? Note we are previously introduced to -> in the previous chapter with the when statement

1 Like

Great question Abunur. I’m not exactly sure why the Kotlin creators chose that type of notation, but you can might be able to find / ask in the Kotlin language support forums.

Also, this StackOverflow question and the Kotlin docs on higher order functions might shed some light on the matter.

1 Like