This Question is Answered

1 "correct" answer available (4 pts) 2 "helpful" answers available (2 pts)
5 Replies Last post: Feb 22, 2012 11:06 PM by Andrey Breslav  
Dot Neter Newbie 21 posts since
Feb 19, 2012
Currently Being Moderated

Feb 20, 2012 8:37 PM

Lambdas

1 Why it doesn't compile?

val sum = {x, y -> x + y}
println(sum(1,2))

code from http://confluence.jetbrains.net/display/Kotlin/Function+literals

is there any plan for local type inference for lambdas?

 

2 What benefits from local functions

fun main(args:Array<String>){

     val sum = {(x : Int, y : Int) : Int -> x + y}

    fun sum2(x:Int, y:Int):Int {return x + y}

}

if language already have lambdas?

Andrey Breslav Apprentice 929 posts since
Jun 11, 2007
Currently Being Moderated
Feb 20, 2012 10:08 PM in response to: Dot Neter
Re: Lambdas

1. Seems to be a bug in the docs. Thanks

BTW, what type would you expect 'sum' to be?

 

2. This makes the language more regular: I can have a function anywhere I like.

Your argument can be extended to "why have functions at all when we can have val's of function types?", and this approach does not seem right.

Andrey Breslav Apprentice 929 posts since
Jun 11, 2007
Currently Being Moderated
Feb 22, 2012 3:13 PM in response to: Dot Neter
Re: Lambdas

Nemerle's type inference is indeed very powerful (and, as a consequence, rather slow). Kotlin aims for a simpler (faster and more predictable) type inference algorithm.

Andrey Breslav Apprentice 929 posts since
Jun 11, 2007
Currently Being Moderated
Feb 22, 2012 11:06 PM in response to: Dot Neter
Re: Lambdas

Yes, it is much more complex, because instead of a single call expression we have to analyze the whole local scope.

More Like This

  • Retrieving data ...