Reputation: 13395
In my code I have a struct like this
post { req ->
with(req.objectBody<Person>()) {
logger.info { "Attempt to save person $this" }
with(require<SessionFactory>().openSession()) {
save(this@with)
}
}
}
But IDE warns me that there is more than one label with such a name
. In this case
save(this@with)
I want to refer to with(req.objectBody<Person>)
instance. How to achieve that?
Upvotes: 2
Views: 69
Reputation: 147941
Technically, you can mark lambdas with custom labels and then use labeled this with those labels. such as:
with(foo()) mylabel@{
with(bar()) {
baz(this@mylabel)
}
}
However, to improve readability, instead of with
, you can use the let
scoping function and provide a name for the parameter:
foo().let { fooResult ->
bar().let { barResult ->
baz(fooResult)
}
}
Upvotes: 6