Is there some function like `with` which is ignori...
# getting-started
t
Is there some function like
with
which is ignoring return value and returns
Unit
? Something like this:
Copy code
public inline fun <T> with(receiver: T, block: T.() -> Unit): Unit {
    receiver.block()
}
Or any better idea how to avoid explicitly returning Unit in this case:
Copy code
fun process(data: Data) = with(data) {
    ///doSomething
    Unit
}
r
You could always just use `apply`:
Copy code
fun process(data: Data) {
  data.apply {
    // doSomething
  }
}
It’ll return
Data
rather than
Unit
but you can just discard that.
(It’s normally used for initialising a legacy object that relies on setters:
Copy code
val person = Person().apply {
  name = ""
  dateOfBirth = TODO()
  ...
}
)
v
You could just specify the return type explicitly:
Copy code
fun process(data: Data): Unit = with(data) {
    // ...
}
2
Or not using `=`e
Copy code
fun process(data: Data) {
    with(data) {
        // ...
    }
}
j
I usually find using a block body a bit clearer in general for functions that return
Unit
(instead of using an expression with
=
)
👍 1
r
Might not be applicable depending on the broader context, but you could also just do
Copy code
fun Data.process() {
   ///doSomething
}
1