https://kotlinlang.org logo
Title
s

Sergey Shnifer

11/15/2018, 9:41 AM
well, i used
<http://handler.post|handler.post>(object: Runnable{
        ...
        })
and this works. Thanks a lot!
👎 1
👍 1
o

Olivier

11/15/2018, 9:54 AM
Please don't, it's an ugly workarround. Kotiln offers lambdas to avoid that kind of writing.
s

Shawn

11/15/2018, 9:57 AM
I wouldn’t be so quick to label anonymous objects as intrinsically “ugly workarounds”. You may be right about there being a better solution here with a Timer, but sometimes you’ll need the object where interface conversions don’t cover every case
I’d agree that @Sergey Shnifer ought to go for a purely lambda-based approach if possible, but there’s nothing wrong about learning how to declare an anonymous class if they’re just going through the book examples for the first time
s

Sergey Shnifer

11/15/2018, 9:59 AM
@Olivier but how to use lambdas if i need self-reference and lambdes do not support "this"? (lets assume that i want to use this handler.post API)
r

rnpy

11/15/2018, 10:01 AM
there's no ugly workaround there, using
object:
is just using less syntactic sugar to write the same code, sometimes it's needed
o

Olivier

11/15/2018, 10:12 AM
@Shawn My bad, using anonymous objects is not a work arround of Kotlin. It's a work arround in this case when the developer wanted to do a repeated job by calling
handler.postDelayed
instead of creating a repeated job.
@Sergey Shnifer Lambdas are not built for using
this
, lambdas are kind of unamed functions, not objects, there no sense to call
this
. I bet if you have to call
this
inside of lambda there is a conception issue, for this case the conception issue was using Handler to do a loop.