Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Suggestion] ktx extensions should return the value to fulfil the chaining pattern #215

Open
martofeld opened this issue Apr 20, 2021 · 3 comments

Comments

@martofeld
Copy link

Feature Request

Without using ktx extensions the code looks as follows

OnItemBindClass<MySealedClass>()
    .map(MySealedClass.Impl1::class.java, /*use either overload*/)
    .map(MySealedClass.Impl2::class.java, /*use either overload*/)

using the ktx extension it should look something like the following

OnItemBindClass<MySealedClass>()
    .map<MySealedClass.Impl1>(/*use either overload*/)
    .map<MySealedClass.Impl2>(/*use either overload*/)

but the .map doesn't return the OnItemBindClass instance like it does in java, therefore the chaining pattern is broken

@evant
Copy link
Owner

evant commented Apr 20, 2021

Agreed

@martofeld
Copy link
Author

I've been playing around with this a little bit, unfortunately since its an extension function we can not access the generic T in OnItemBindClass<T>. Therefore you need to re-state the T. This is the best I could get to.

inline fun <reified E: T, T> OnItemBindClass<T>.mapKt(
    variableId: Int,
    @LayoutRes layoutRes: Int
): OnItemBindClass<T> =
    map(E::class.java, variableId, layoutRes)

Usage

OnItemBindClass<Any>()
        .map<String, Any>(BR.item, R.layout.item_header_footer)
        .map<MutableItem, Any>(BR.item, R.layout.item)

If such is the case, I think the apply solution will still be the best

@lhjgege
Copy link

lhjgege commented Sep 12, 2024

I've been playing around with this a little bit, unfortunately since its an extension function we can not access the generic T in OnItemBindClass<T>. Therefore you need to re-state the T. This is the best I could get to.我一直在玩这个,不幸的是,由于它是一个扩展函数,我们无法访问OnItemBindClass<T>中的通用T 。因此您需要重新声明T 。这是我能达到的最好的。

inline fun <reified E: T, T> OnItemBindClass<T>.mapKt(
    variableId: Int,
    @LayoutRes layoutRes: Int
): OnItemBindClass<T> =
    map(E::class.java, variableId, layoutRes)

Usage 用法

OnItemBindClass<Any>()
        .map<String, Any>(BR.item, R.layout.item_header_footer)
        .map<MutableItem, Any>(BR.item, R.layout.item)

If such is the case, I think the apply solution will still be the best如果是这样的话,我认为 apply 解决方案仍然是最好的

It can be changed to this way more convenient to use
inline fun <reified E : Any> OnItemBindClass<Any>.map( variableId: Int, @LayoutRes layoutRes: Int ): OnItemBindClass<Any> = map(E::class.java, variableId, layoutRes)

Usage

val item=OnItemBindClass<Any>() .map<String>(BR.item, R.layout.item_header_footer) .map<MutableItem>(BR.item, R.layout.item)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants