https://kotlinlang.org logo
Title
l

louiscad

01/11/2018, 9:31 PM
Hi all, what would you choose?
inline fun View.onClick(crossinline l: (v: View) -> Unit) = setOnClickListener { l(it) }

inline fun View.onClick(noinline l: (v: View) -> Unit) = setOnClickListener(l)
The first one is optimized but doesn't gives you a proper stacktrace for the
l
lambda in case you want it, if you get an exception for example. The second one generates 2 classes instead of one, and has 3 methods calls instead of one when a click strikes, but you see it all in the stacktrace, with the exception source. Note that the second one still produces meaningful method and class names. Just the line number is plain wrong and misleading (not hard to figure out you can't trust it).
MainActivity$onCreate$$inlined$onClick$1.onClick
1️⃣ 2
w

w_bianrytree

01/12/2018, 10:17 AM
Or maybe you can just debuging with
noinline
and change that into
crossline
when you want to release?
l

louiscad

01/12/2018, 12:01 PM
@w_bianrytree That's a nice suggestion! However, I'm afraid I can't take advantage of it as this code will be put in a library
d

dave08

01/14/2018, 3:46 AM
I think 1 is right since I hope most use mvvm or mvp and don't put much logic in the onClicks... so any ways it will be delegated (then with 2 you'd have 4 method calls...).