Public review of the Standard Library APIs [Closed]

We keep having a hard time with T.run vs. with vs. let. These three “global” functions sound very different, but actually only differ in “cosmetics”. Almost always any of them can be used, and it’s unclear which to prefer. This regularly leads to debates in code reviews. Furthermore, it’s not intuitive why it’s with(foo) vs. foo.run. (In fact, this seems to be the only difference between the two!)

On the other hand, we sometimes miss the ability to access the receiver of apply as a function argument. For example, val foo = Foo().apply { configure(it) } feels more natural than val foo = Foo().apply { configure(this) }. We use apply more often than any of the other methods.

I hope there is a way to unify these methods, or make their similarity and distinctions more apparent, that removes a lot of this guesswork. I made one proposal in Let vs. with vs. run - #2 by ilya.gorbunov. Another idea that comes to mind is to remove one of T.run and with, although this still leaves with vs. let.

If no other solution is found, damage could be reduced by having KDoc explain the rationale and intended use cases for each method, rather than just paraphrasing the code.

3 Likes