Kotlin let analogue which returns the receiver

93 views Asked by At

I was wondering if there's a standard function which behaves similarly to let but returns the receiver itself, not the value calculated by block.

My piece of code with let:

fun rightParts(s: Lexeme) = 
    rightPartsByLeft[s] ?:
    ArrayList<Rule>() let { rightPartsByLeft[s] = it; it }
                                                      ^ here's the statement which I would
                                                        like to avoid

So, if there would be such a function named, for example, btw, the code above would shorten a little and become more expressive:

 fun rightParts(s: Lexeme) = 
     rightPartsByLeft[s] ?:
     ArrayList<Rule>() btw { rightPartsByLeft[s] = it }

Of course, I might write this function by myself and include it into every project, but I suppose, people would like a function with such semantics.

Or is there another nice way of doing the same in Kotlin?

0

There are 0 answers