Library for more scoping functions

For the topic below, I wonder whether there are built-in language features to solve this more nicely - or whether there are existing libraries containing scoping functions like this. If not I think about creating one and wonder whether there’s an interest in this.

Quite frequently in projects I end up adding extra scoping functions to have nicer code.

Example:

I have a Builder class which method chaining (like many classes in Spring). I only want to perform an action if an input parameter is set (e.g. a controller parameter).

fun get(@Requestparam param: String?) {
   val result =
       Builder
           .func1()
          .apply { param?.let { func2(param) } }
}

I was looking for ways to make this more readable. One Idea I came up with was to define my own scoping function:

inline fun <T, P> T.xapply(param: P?, block: T.(P) -> Any): T {
    if (param != null) block(param)
    return this
}

Which would allow to write something like this:

fun get(@Requestparam param: String?) {
   val result =
       Builder
           .func1()
          .xapply(param)  { func2(it) }
}

or

fun get(@Requestparam param: String?) {
   val result =
       Builder
           .func1()
          .xapply(param, Builder::func2)
}

Can this be achieved in a similar way by language features or are there libraries allowing a similar syntax?

Why do you need libraries if you’ve already written an extension that solves your problem?

Also, block should return Unit, not Any.

Kotlin’s scoping functions aren’t really a language feature, they are just normal functions in the stdlib. They can be implemented anywhere and don’t require any special treatment by the compiler.
If you have scoping functions that you think are very usefull I suggest implementing them yourself.

I guess you could suggest adding them to the stdlib, but I don’t really like the idea of a function named xapply. I can’t tell from a simple look what it does and I feel this function will just lead to confusion for most people.

The usage is too narrow for stdlib. And it requires two lines, so everyone can do it themselves.

1 Like

I guess you could suggest adding them to the stdlib, but I don’t really like the idea of a function
named xapply . I can’t tell from a simple look what it does and I feel this function will just lead to
confusion for most people.

right, stdlib, sry.

Thanks for your feedback. naming and so forth was just for demonstration purposes, so that’s definitely something that would require improvement. Also, I’m not sure whether the use case is too special for the stdlib.

@darksnake

Why do you need libraries if you’ve already written an extension that solves your problem?
The usage is too narrow for stdlib. And it requires two lines, so everyone can do it themselves.

Right - and I now ended up implementing it in three different projects. Following the rule of three, I wondered whether a common implementation outside of my personal pond would be reasonable. Thanks for your feedback on this.

I can see the point of having something like that. I don’t think it is something that should be in the standard library, but a common utility library could have this easily.

There is some effort put into GitHub - kotlin-extra-library/kotlin-extlib: Kotlin community common multiplatform library. One can contribute there. Personally, I am usually just implement those utils in the project. But I mostly write libraries, so it is quite simple for me to put utilities in the bottom level library.

1 Like

Thanks. Was looking for something like that. Unfortunately it seems that there’s little activity on that project. Another project with a similar scope I found was GitHub - vanshg/KrazyKotlin: A collection of useful Kotlin Extension, but there is hardly any activity on that one either.

I would prefer to contribute something to a project like these. Is anyone aware of a similar project with has some more activity lately?

Well it active if someone contributes to them. As far as extlib concerned, I know that @lamba92 is pretty active around community.