https://kotlinlang.org logo
#announcements
Title
# announcements
s

spand

03/08/2017, 3:33 PM
Ie. something like
String.{ trim() }
so it would have the type
String.() -> String
r

robin

03/08/2017, 3:35 PM
val test: String.() -> Unit = { trim() }
And if you're not using the lambda in an assignment it's probably in a function call, where type inference should make the explicit declaration unnecessary.
s

spand

03/08/2017, 3:39 PM
My usecase is something like this
Copy code
val foo : String.() -> Unit = when(false){
                        true -> {}
                        false -> {}
                    }
and it fails to infer
l

lupajz

03/08/2017, 3:51 PM
You are not returning your function there
Copy code
val foo : String.() -> Unit = when(false){
    true -> { {} }
    false -> { {} }
}
👍 1
m

mfulton26

03/08/2017, 3:56 PM
Copy code
val foo = when (false) {
    true -> fun String.() {}
    false -> fun String.() {}
}
r

robin

03/08/2017, 4:11 PM
@lupajz Is right, in your example the braces are treated as block delimiters for the when-branch, not as lambdas.
l

lupajz

03/08/2017, 4:14 PM
It would be really cool if this worked sometime in future. I would love not have to write 2 sets of braces 😕
r

robin

03/08/2017, 4:21 PM
@lupajz You could do it this way:
Copy code
val foo : String.() -> Unit = when(false){
    true -> { -> }
    false -> { -> }
}
Same amount of characters to type, but at least it's a bit easier to read imo.
👍🏻 1
l

lupajz

03/08/2017, 4:27 PM
@robin that’s nice! never used that thanks
r

robin

03/08/2017, 4:28 PM
Me neither, but your comment made me wonder whether the arrow notation works even with no parameters, turns out it does 😄
s

spand

03/09/2017, 7:27 AM
Lovely. Thanks for the replies !
3 Views