https://kotlinlang.org logo
Title
a

Allan Wang

09/29/2018, 1:00 AM
Is it possible to take in a lambda that is based around generics? For example, can we simplify the following:
enum class Order {
   ASCENDING {
       override fun <T : Comparable<T>> sort(iterable: Iterable<T>): List<T> =
               iterable.sorted()
   }, DESCENDING {
       override fun <T : Comparable<T>> sort(iterable: Iterable<T>): List<T> =
               iterable.sortedDescending()
   };

   abstract fun <T : Comparable<T>> sort(iterable: Iterable<T>): List<T>
}
Such that it is something in the form of
enum class Order(...) {
  ASCENDING(::sorted), DESCENDING(::sortedDescending)
}
v

Vladyslav Sitalo

09/29/2018, 9:08 AM
a

Allan Wang

09/29/2018, 3:09 PM
I've used sealed classes before, but I don't think that solves the problem of taking in a generic method where the generic is unrelated to the class
The goal is to avoid something like
class ASCENDING<T : Comparable<T>>(val sort: (Iterable<T>) -> List<T>)