Syntax of functions


#1

I was wondering about the syntax of delegates for properties. Actually, It's probably more about the syntax of functions. This one works as expected:

var x: String by Delegates.observable("initial") { meta, old, new ->
        System.out.println("Changed ${meta.name} from $old to $new")
}

And this one doesn't:

 
var x: String by Delegates.observable("initial") { meta, old, new -> {
        System.out.println("Changed ${meta.name} from $old to $new")
    }
}

Why is that the case?


#2

This one:

 
{ meta, old, new -> {
        System.out.println("Changed ${meta.name} from $old to $new")
    }
}

defines a lambda which returns lambda.


#3

Thanks! The second parameter is (desc: PropertyMetadata, oldValue: T, newValue: T) -> Unit. So I figured that it shouldn't even compile because it returns a lambda expression instead of Unit?


#4

Actually, the type of outer lambda is (PropertyMetadata, String, String) -> Unit (because observable expects function returning Unit). So the dummy lambda is created, but not used anyhow. Usually there should be warning that lambda is not used, but now it's not shown here, and it's a bug: KT-5549