Hey, I’ve racked up about 15 minutes of Kotlin experience, and am wondering about with()
.
In this Android developer page (which is just what I happen to be using in experimenting with Kotlin), the “Show the notification” section about a third of the way down the page has this example:
with(NotificationManagerCompat.from(this)) {
notify(notificationId, mBuilder.build())
}
First, that is roughly equivalent to this, right? (They seem to do the same thing.)
NotificationManagerCompat.from(this).notify(notificationId, mBuilder.build())
If so, is the first approach the generally preferred way to do it in Kotlin (and if so, why), or is the author of that article a weirdo? (I acknowledge that those two statements are not mutually exclusive.)
Second–and unrelated, I guess, but you’re already here–I gather that these two are equivalent:
// block outside parentheses
with(foo) { bar() }
// block inside parentheses
with(foo, { bar() })
Looking at the signature for with()
, the second is what I would expect to see, but the compiler gives a warning, saying the lambda should be moved outside the parentheses. Why?! (I notice that, in the List.fold()
example here, the lambda is inside the parentheses; pasting that into the compiler gives the same warning.) If a method takes two arguments, putting one inside the parentheses and one outside rubs me the wrong way!