I have this method: `fun click(onClick: ((v: View)...
# getting-started
r
I have this method:
fun click(onClick: ((v: View) -> Unit)? = null) {}
What is the correct syntax for describing
click(if (condition) null else <a lambda>)
v
rockerhieu: That is the correct syntax
r
I could make it work
*couldn't
v
click(if (1 == 1) null else {})
must work
if it doesn't , then post the error messages and we will look at them
f
this will work
Copy code
val r: ((View) -> Unit)? = if (true) null else { view -> println(view) }
click(r)
r
Here is the error http://imgur.com/a/gvyr6
@forcelain that what I did as a workaround, but I'd expect @voddan 's suggestion should work also
f
I see 2 problems here: 1) it’s not obvious for the compiler if bracers in
else {}
is a lambda or just a statement bracers 2) you should explicitly specify that null is
((v: View) -> Unit)?
so, this also works
Copy code
click(
            if (true) {
                null as ((v: View) -> Unit)?
            } else {
                { }
            })
r
Interesting, it works, thanks @forcelain. Although the IDE suggested me to remove the useless cast, but without the explicitly cast it doesn't work
f
yep
v
Strangely enough nothing that makes sense works in this situation. Could you report it to youtrack.jetbrains.com/issues/KT please?
r
Sure, doing
👍 1