Is with() appropriate for operating on function result?


In my quest to be more functional, I’m trying to use data classes in an immutable way and write my methods more functionally. Let’s say I have the following method:

fun mutateDataObject(o: object): object

That is, it receives a data object, mutates it, and returns the mutated object.

I would prefer to not have to create new val declarations every time I call one of these methods, and I have been experimenting with using a with() block to contain the result:

with(mutateDataObject(a)) {
    if this.member ==1 then return "yes" else return "no"

Is this a valid use of the with() structure in Kotlin?


It’s more or less the exact reason with() exists.