Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Make indexedEnum comparable #10

Closed
pedrorijo91 opened this issue Dec 3, 2016 · 4 comments
Closed

Make indexedEnum comparable #10

pedrorijo91 opened this issue Dec 3, 2016 · 4 comments

Comments

@pedrorijo91
Copy link

Since indexedEnums have an index, it should be easy to sort them. Maybe by adding extends Ordered[] and defining the .compare method using the index value?

@utaal
Copy link
Contributor

utaal commented Dec 7, 2016

IndexedEnum is parametrised over an unconstrained type T for the index and T is not necessarily T : Ordered (it could be a custom type without natural ordering), so I would avoid forcing Ordered onto all ItemizedIndices.
Do you have an explicit use case for this? If so, we can take a look at how to do this cleanly.

@pedrorijo91
Copy link
Author

I've been comparing some approaches for enumerations in scala and I've found this 'feature' that I could not find an easy way to make it work with itemized.

While it's not a very common use case, it may happen. If you wish to leave the issue here until someone else complains feel free (or to close until someone reopens it).

@utaal
Copy link
Contributor

utaal commented Dec 7, 2016

I'd be interested in seeing how others deal with this, do you have a link to a library that supports Order?

@pedrorijo91
Copy link
Author

for instance, enumeratum, does not supports Order itself, but it's implementation doesn't block easy workarounds:

sealed abstract class Weekday(val order: Int) extends EnumEntry with Ordered[Weekday] {
   def compare(that: Weekday) = this.order - that.order
 }

 object Weekday extends Enum[Weekday] {
   val values = findValues

   case object Monday extends Weekday(2)
   case object Tuesday extends Weekday(3)
   case object Wednesday extends Weekday(4)
   case object Thursday extends Weekday(5)
   case object Friday extends Weekday(6)
   case object Saturday extends Weekday(7)
   case object Sunday extends Weekday(1)
 }

this is somehow related with #11

@utaal utaal closed this as completed Jul 10, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants