Function literal limitations


#1

I understand there's a wish to make Kotlin well suited for making DSL's just like the ones in Ruby and perhaps even Scala.

But I’m unsure about wether the current limitations are because of type infereness problems or grammar problems.
It seems that “bad1” below could suffer from the features used to omit parens, for example.

I found it amusing that the workaround with printInt2 seems to work fine, so it a grammar issue after all? :slight_smile:
Too bad one can’t write a function “wrap” such as “wrap printInt” creates printInt2.

package Main

fun main(args: Array<String>) {
  fun printInt(x: Int) = println(x)
  // Trick to be able to use printInt below
  fun printInt2() = {(x: Int) -> println(x) }

  // There’s no support for this yet, recommendation is to
  // use a function literal instead
  val bad1 = printInt
  val ok1 = printInt2()

  // So, using a function literal, but “it” is unresolved despite full
  // type information about printInt is available
  val bad2 = { printInt(it) }

  // While this works fine
  val ok2: (Int) -> Unit = { printInt(it) }
}


#2

bad1 case is just a feature to be implemented later, this feature is all you need in fact, so all the rest does not seem to matter that much.

bad2 does not generalize to anything more complex than a single call to a function having no overloads: the type information is available in this simplistic case, but nothing more complex will work, so there’s no point in supporting this at all.