Home > database >  What kind of parameter that can pass variables inside a Unit in Kotlin?
What kind of parameter that can pass variables inside a Unit in Kotlin?

Time:10-01

I am confuse about what kind of parameter should I pass from the main class to the recycler adapter class that have parameter type like this

class RecyclerAdapter(private val clickListener:(view: View, movieName: String)
    ->Unit):ListAdapter<MovieInfo,RecyclerAdapter.ViewHolder>(Diff())

In the main class, my plan is to declare the adapter like below

val adapter = RecyclerAdapter() //missing parameter inside the ()

I have never seen a class parameter like that before. Usually, it is a starightforward list data type such as RecyclerAdapter(private val dressList: List<DressList.DressName>). Maybe if I know some examples about that kind of parameter, it can give me some enlightenment about what parameter should I pass. Thanks.

CodePudding user response:

private val clickListener:(view: View, movieName: String) this is basically call back function and ->Unit is lambda function where Unit represent return type of function

Unit in kotlin same working as in java a void type

CodePudding user response:

You should read the documentation on functional type syntax and lambda syntax to understand how to interpret this. You don't need to be doing anything with Unit. Unit is the type your lambda should be returning.

The functional type in this case has a return type of Unit. That just means it doesn't return anything useful. Unit is the default return type of any function that doesn't really return anything, so when you see ->Unit as part of the function type, it means your lambda doesn't need to return anything.

In this case, you can use Trailing Lambda Syntax (which is also explained in the above link), which means you can omit the parentheses. Something like this:

val adapter = RecyclerAdapter { view, movieName -> 
     // your code for what to do whenever an item in this adapter is clicked
}

I don't know what you were trying to do with fold in your comment. The documentation uses fold as an example of a higher order function, so it is showing you how to call a higher order function. It is not telling you that you should use fold to create a functional type! fold is just an example of a higher order function. Your RecyclerAdapter constructor is also a higher order function.

CodePudding user response:

Just to break this down

val name: String

That's simple enough - the variable is called name, and its type is String. If that's a parameter, you need to pass a String there.

val clickListener: () -> Unit

The type here is () -> Unit, which is a function. You know how you call a function by putting () after the name, like toString()? That's what that represents in the type, a function that takes no parameters. The -> is showing the type it produces (or returns if you like). This is a function that takes no parameters, and produces no value (aka Unit).

val clickListener: (view: View, movieName: String) -> Unit

This is similar, except this type is a function that does take parameters - a View and a String, and returns nothing. When you call this function, you're expected to pass those values into it, and the function does something with them.


The setup you have here is basically an Adapter that's displaying MovieInfo objects, and in the constructor you pass in a listener function. So when the user clicks an item, that function gets called with the relevant values. It means a component outside of the adapter can decide what actually happens when something gets clicked, by providing a function that does the necessary stuff. The adapter doesn't need to know any of those details - it just needs to pass the listener function the data it requires.


I don't know what you're doing with this:

val clickListener = movieList.fold(Unit, { view, movieName -> Unit })

but if you hover over clickListener you'll see its inferred type is Unit. This isn't a function object, this is the result of calling the fold function. You've run fold over your movieList, basically returning Unit at each step, and the result is Unit. That's what you're trying to pass as your clickListener parameter.

A function would be in { } braces (or a function reference like ::handleClick), like this:

val clickListener = { view, movieName -> // do stuff that returns unit }

and if you really wanted to do a fold for some reason, you can do it in there! But it's usually a function that does something like display a movie, add it to some list or a database, that kind of thing. An action in response to a click

  • Related