Can kotlin have expression function body block?


#1

Next expressions results in a function return type:

val x = {
  prinln(par)
  “>>$par”
}

fun test(par:String) = {
  prinln(par)
  “>>$par”
}

I think that the next is more apropriated to do this:

val x = {->
  prinln(par)
  “>>$par”
}

fun test(par:String) = {->
  prinln(par)
  “>>$par”
}

class Test{
  {//this block is executed, not in conformance with above described behavior
           val x = “test”
           prinln(x)
           “>>$x”
  }

  fun t() = System.currentTimeMillis()

  fun test(){
  var last = t() + 10000
  var time = 0l

          while( {time = t() ; time}() < last ){ //closure inside must dont need () to be executed, this can be infered
           println(“time is: $time”)
          }
  }
}

Conclusion:
  Accept a block expression as function literal only if inference prove that is the expected type, otherwise execute it.
  function body that follow func x() = {…} syntax, must be treated like expressions.


#2

Is it a question, a comment or a proposal?


#3

Im new in kotlin, when i seen kotlin i told to my self "it is what dart should be", i have background in (java, scala, groovy, xtend, ruby, dart), and when i talked to dart forum and said that dart are losting the opportunity to be a new language and not an repaginated java for the web.

When i try to convince scala people to migrate to kotlin, like me,
functional style of expression method(aka function) body is the most intriguing question,
a more compact declaration of property is another, but less important.

Has a more appropriate place to submit proposals for kotlin?


Best regards.


#4

Regarding your proposals: 1) I don't see how the user benefits from a required arrow in a lambda expression 2) The syntax for anonymous class initializers is likely to be changed so that they do not resemble blocks any more 3) If you want to execute a block of code, there is a standard function for this: run()

val foo = run {...}


#5

I see that in byte code is generated an overhead, if is no mean to create a behavior like scala methods the compiler can strip to make byte code equals method internaly.

inline fun <R> _(f: () -> R) = f()

class Hello {
  val xx = _{  “SXS” }

  fun test(x : Int ) = _{
  val res = “x=$x”
  println(res)
  res
  }
}


#6

That will be covered by inline functions soon


#7

Considering code block (including function body) an expression is expected for language in which "if" is an expression.

As Kotlin tries to make lambda from almost everything denoted as “{…}”, it looks difficult to handle, for example, such a code, meaning we want x to be 9, not a function returning 9.

val x = {
  val y = 3
  y * y
}

Using “run” stdlib function covers this situation for both internal blocks and function bodies. Moreover, now decompilers show that Kotlin succeeds when optimizing run { … }. Nevertheless, I personally still think that expression blocks will look very nice in Kotlin. Explicit “return” operator is a sort of “goto” in its nature, and making developers writing it is unmerciful a bit =).

So allowing them for “fun” construction without “… = run …” can be very useful (hope it will not be very difficult to parse). And may be “run” function can be part of the language, not stdlib, to emphasize its value.