https://kotlinlang.org logo
#getting-started
Title
# getting-started
l

Lukasz Kalnik

11/09/2023, 12:21 PM
Why cannot I pass a function reference to
Iterable<T>.sortedBy()
?
Copy code
fun mySort(uiState: UiState): Int = // sort based on some criteria

val uiStates: List<UiState>

uiStates.sortedBy(mySort) // doesn't compile: Function invocation `mySort(...)` expected; No value passed for parameter `uiState`
OK, forgot that function references need to be prefixed with a double colon 🤦 This works:
Copy code
uiStates.sortedBy(::mySort)
😄 2
rubber duck 5
t

Travis Griggs

11/10/2023, 12:51 AM
ok... why a rubber duck emoji? I'm missing a cult reference here. or at least how it applies (I'm running through the James Veitch duck sketch and trying to figure out how that apply here)
l

Lukasz Kalnik

11/10/2023, 8:05 AM
It's a debugging technique, where you explain the problem to yourself loud (e.g. talking at a rubber duck) to find a solution: https://en.wikipedia.org/wiki/Rubber_duck_debugging
🙏 1