As Lym Zoy says it
is the implicit name of the single argument to a closure.
If you are not familiar with closures and higher order functions in Kotlin, you can read about them here . A function that take a closure/function/lambda, is one that is basically asking for some help to do it's job.
I like using sortedBy as a good example. sortedBy
is a function in the Kotlin collections library that will sort a collection, but in order for it to work it needs a comparable attribute for each item. The way it solves this is it asks you, the user of the sortedBy function, to provide a function that takes a member of the collection and returns a comparable attribute. For example if the collection was a list of Person objects, you could provide sortedBy
a different closure if you wanted to sort by firstName, lastName or age.
Here is a quick example which you can also find here , which shows how sortedBy
can take a closure argument which takes a member and returns a comparable attribute which sortedBy
can use to rank the various members of the collection. In the first case the closure/function returns the age of the member and in the second case the closure returns the lastName (using the implicit form), both of which are Comparable, an Int and a String.
data class Person(val firstName: String, val lastName: String, val age: Int)
fun main(args: Array<String>) {
val people = listOf( Person("Jane", "Jones", 27), Person("Johm", "Smith", 22), Person("John", "Jones", 29))
val byAge = people.sortedBy { person -> person.age } // explicit argument: person is a memeber of the List of Persons
val byLastName = people.sortedBy { it.lastName } // implict argument: "it" is also a member of the List of Persons
println(people)
println(byAge)
println(byLastName)
}
Getting back to the detail of your specific question.
In your question the parseList function found here is defined as such:
fun <T : Any> SelectQueryBuilder.parseList(parser: (Map<String, Any?>) -> T): List<T> =
parseList(object : MapRowParser<T> {
override fun parseRow(columns: Map<String, Any?>): T = parser(columns)
})
This is a function that takes a closure that expects one argument of type Map<String, Any?>
So in the call shown in your question:
.parseList { DayForecast(HashMap(it)) }
where { DayForecast(HashMap(it)) }
is the closure expected to be passed to parseList,
the longer form { arg -> DayForecast(HashMap(arg) }
could have been used as well but the short form { DayForecast(HashMap(it)) }
is the more idiomatic form where using it
as the argument allows you to skip the arg ->
part.
So in this case it
is a Map object provided by the parseList
function. The object referred to by it
is then passed as the lone argument to a HashMap constructor (which not surprisingly expects a Map), the result of that construction, is then being passed to the constructor of DayForecast