any working code example maybe? :wink:
# announcements
a
any working code example maybe? 😉
âž• 1
s
lol sure, if you’d really like
in the call to
<http://khttp.async.post|khttp.async.post>()
there’s an optional
onResponse
param that takes
Response.() -> Unit
oh, guh, actually, this version of the code doesn’t use
.let
initially I was under the impression that
JSONObject.getString()
with a nonexistent key would return
null
, so for a bit there, the last line was a
?.let { log }
a
😄
maybe you can contrive a really simple example?
like 2-3 lines of code
s
hm, might take me a sec to match the exact weirdness of the original situation
here, though it’s 11 lines
Copy code
interface Foo {
  val string: String?
  fun frobnicate(block: Foo.() -> Unit)
}

fun consume(foo: Foo) {
  val baz = { lad: Foo ->
    lad.string?.let(::println)
  }
  foo.frobnicate(baz)
}
that yields a compiler error
setting
val baz: (Foo) -> Unit
fixes it
or tacking on
?: Unit
onto the
let
line
a
yeah, nice example, that makes it clear 🙂
I also think explicit type would be best here
how about something like this:
Copy code
fun consume(foo: Foo) {
    val baz: Foo.() -> Unit = {
        string?.let(::println)
    }
    foo.frobnicate(baz)
}
although that might apply only when the lambda is with receiver
g
+1 for explicit type
It’s also good for such lambda declarations with other types
otherwise it’s to easy implicitly change type of variable with lambda