AlexD
12/08/2021, 11:12 PMrun vs let for this specific case
1️⃣
fun getFirstAddress(list: List<Coordinates>): Address? {
// ... do something with list
return list.firstOrNull { it.isValid() }?.run { Address(latitude, longitude) }
}
2️⃣
fun getFirstAddress(list: List<Coordinates>): Address? {
// ... do something with list
return list.firstOrNull { it.isValid() }?.let { Address(it.latitude, it.longitude) }
}
3️⃣
fun getFirstAddress(list: List<Coordinates>): Address? {
// ... do something with list
return list.firstOrNull { it.isValid() }?.let { coordinates ->
Address(coordinates.latitude, coordinates.longitude)
}
}
4️⃣
fun getFirstAddress(list: List<Coordinates>): Address? {
// ... do something with list
return list.firstOrNull { it.isValid() }?.toAddress()
}
private fun Coordinates.toAddress() = Address(latitude, longitude)
5️⃣
fun getFirstAddress(list: List<Coordinates>): Address? {
// ... do something with list
val firstValidCoordinate = list.firstOrNull { it.isValid() } ?: return null
return Address(firstValidCoordinate.latitude, firstValidCoordinate.longitude)
}Joffrey
12/08/2021, 11:15 PMfun getFirstAddress(list: List<Coordinates>): Address? {
// ... do something with list
return list.firstOrNull { it.isValid() }?.toAddress()
}
private fun Coordinates.toAddress() = Address(latitude, longitude)Paul Griffith
12/08/2021, 11:41 PMAddress that directly accepts Coordinates, then ?.let(::Coordinates)
I like 4 for readability, although it's a fairly minor gain in this case
I feel like I would probably actually write this as 2 first and only rewrite it to 4 if I was really bothered by itelizarov
12/09/2021, 7:14 AMlet if you don’t have an extension function. With extension function it is much nicer, though.Matteo Mirk
12/09/2021, 8:27 AMAlexD
12/09/2021, 8:45 AMit all over the place when using let . The code would be more concise with run, wouldn't it?Joffrey
12/09/2021, 8:48 AMrun IMO. I consider let to be "the map for single values", which would be the appropriate alternative here if not using an extension. If you really have a lot of `it`s (meaning the expression in the let is big), it might be worth using extensionsAlexD
12/09/2021, 8:59 AMrun is intended for some specific purposes I really wish we had another let function similar to run say letItMatteo Mirk
12/09/2021, 9:07 AMis useful when your lambda contains both the object initialization and the computation of the return value.run
Joffrey
12/09/2021, 9:07 AMrun. There is real no reason for an extra scope function in the stdlib here.
The point of let being considered as map for single objects is that it really does behave like map as well, with it in the lambda.Daniel
12/09/2021, 5:47 PMfun getFirstAddress(list: List<Coordinates>): Address? {
// ... do something with list
val firstValidCoordinate = list.firstOrNull { it.isValid() } ?: return null
return Address(coordinates.latitude, firstValidCoordinate .longitude)
}
No need for nesting. This could be solved by a guardDaniel
12/09/2021, 5:48 PMAlexD
12/09/2021, 6:18 PMrun or let ? This brings me a question about the kotlin way. Are we more tolerant of using variables than the scope functions?Daniel
12/10/2021, 6:41 PMAlexD
12/10/2021, 11:15 PMgildor
01/03/2022, 4:11 AM