Can't pass a fun


I was surprised to learn that the following Kotlin code does not compile

fun outer (x : Int) : (Int) -> Int {   fun inner (y : Int) : Int {   return x + y   }   return inner }

Instead, one has to write

fun outer (x : Int) : (Int) -> Int{   val inner = {(y : Int) : Int ->   x + y   }   return inner }

While the second form may be "more correct" in some sense, it is less clear (inner is declared as a val, not a fun). Any reason not to support the first form, as Python does?


You should say "::inner" instead of "inner" to indicate that your function is passed as a value. This will make your first example work.


Nice. Although from a language aesthetics POV, I don't see why `return inner` doesn't automatically resolve to `return ::inner`. The nitpicker in me is asking whether the extra fuss serves a purpose. I'm pretty sure it doesn't 'cause if it did, Python would have a problem.


Don't forget that Python is a dynamic language. For one thing, it doesn't have overloading, and :: syntax reserves space for overload disambiguation. Another thing is that :: may create a new object, and that may have a performance impact, which Python users are likely to care little about, but JVM users prefer to see.


Makes sense. Thanks for the explanation.