Kotlin lambdas and DSL support?
See original GitHub issueHi, Thanks for this great library. When using with kotlin, I feel it’s a bit boilerplate can be remove by using kotlin DSL and Lambda feature. For example a GenericItem:
typealias BindFunction = ViewHolder.(position: Int) -> Unit
class GenericItem(@LayoutRes val layoutId: Int, val binder: BindFunction) : Item() {
override fun bind(viewHolder: ViewHolder, position: Int) {
binder(viewHolder, position)
}
override fun getLayout(): Int {
return layoutId
}
}
// when use it
val item = GenericItem(R.layout.item_layout){ position ->
// the bind code
}
By that way I can just use a GenericItem without creating a new subclass of Item.
Even more with kotlin DSL style support. We can easily build a DSL style List for RecyclerView For example :
val adapterGroupAdapter<ViewHolder> = goupieList {
item {
layoutId = R.layout.item_1
binder = {
//bind code
}
}
item {
layoutId = R.layout.item_2
binder = {
//bind code
}
}
item {
layoutId = R.layout.item_3
binder = {
//bind code
}
}
}
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:11 (6 by maintainers)
Top Results From Across the Web
Kotlin: Create your own DSL using Lambda expressions
Unlike general purpose languages (such as Kotlin, Java, C, C ++, etc.), DSLs are created to solve problems in a given domain, or...
Read more >High-order functions and lambdas | Kotlin
Kotlin functions are first-class, which means they can be stored in variables and data structures, and can be passed as arguments to and ......
Read more >Domain-Specific Languages In Kotlin: Getting Started - Kodeco
In this Kotlin tutorial, learn how to create a DSL using Kotlin lambdas with receivers, builder pattern and extension functions!
Read more >Chapter 11. DSL construction - Kotlin in Action
Kotlin DSL design relies on many language features, two of which we haven't yet fully explored. One of them you saw briefly in...
Read more >mtumilowicz/kotlin-dsl-lambda-workshop - GitHub
Simple DSL in Kotlin with introduction to basic features. - GitHub - mtumilowicz/kotlin-dsl-lambda-workshop: Simple DSL in Kotlin with introduction to basic ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m open to the idea of better Kotlin support, was thinking about it today … but I’m not convinced making Items on the fly is a major use case for Groupie. In fact, I wrote it to encourage code reuse.
Do you normally make one-off Items like this? I never have …
I ended up quite liking this idea. What I have decided to do, in case groupie wants to implement similar, is to show a default item in case a setter method isn’t called. As with the header of an expandable group.
I use group to open up a basic group builder which returns a single group.
I realize that I often display lists of data, so I created a method to allow us to map over a list, with the GroupBuilder.
fun <T> map(data: List<T>, init: GroupBuilder.(T) -> Unit): CompoundGroup
One of my issues has been calling methods on your parent, as with expanding a group upon touch of the header. With the below implementation it becomes simple to get the parent instance:
this@expandable.instance
I do this by having a public field which is set the moment the build method is called on the builder.override fun build() = CompoundGroup(groups).also { _instance = it }