I’m not sure “EnumValueSet” should be taken verbatim.
It’s for now just a placeholder for some concept that needs further definition.
API-wise I think I would prefer some compiler generated object member (which likely implements that “EnumValueSet” type) on an enum’s companion object. So the API would look more like $SomeEnumType.ValueSet
(for the “EnumValueSet” value, and as it’s an object, $SomeEnumType.ValueSet.type
for the type) instead of being a type parametric constructor. But maybe having an exposed stand-alone constructor has some benefits?
Despite that, the main question is still: What is this “EnumValueSet”?
It could be completely opaque (and maybe even “pure virtual” in the sense that it’s just a compile time abstraction), and for example just offer some asTuple
and asSet
methods. (Which would be than kind of magic, I guess).
It could be also a dedicated new type of Set
, which all the collection API. Maybe than with some features from tuples also?
Maybe it could be also something else I didn’t think of, like something in between the two mentioned possibilities.
I’m not sure what’s most feasible from the technical standpoint anyway. Also the mapping to Java isn’t clear. Maybe the compiler could generate somehow two things at once: In case the Scala enum extends Java’s Enum a hidden “values()” method, and in any case for regular use in Scala that .ValueSet
object member on the enum.
While prove reading this, I noticed: Maybe this “EnumValueSet” is actually unneeded at all.
What if I could use some compiler generated $SomeEnumType.toSet
or $SomeEnumType.toTuple
methods to get directly the enum values as the specified data structure?
Such API looks very Scala-ish, returns nice and idiomatic data structures specific for a use-case, and this would avoid to create a new data type at all.
I can’t come up with some example where one wants to look at an “EnumValueSet” as a Set[$SomeEnumType]
and a (EnumValue1, EnumValue2, <etc>)
tuple at the same time. So maybe having just the two mentioned methods would be sufficient.
But than you had at least two methods to get the values of an enum… Each with a very specific use-case in mind (rich collection API vs. accessing very precise type information). Even likely simpler to implement this looks more involved when it comes to teaching / learning. Kind of back to square one. (At least you would get proper data types, instead of a mutable Array…)
Still I’m not sure about the value of a dedicated set-like data type which is able to carry exact type information about each of its elements. Is something like that useful outside of the “EnumValueSet” use-case? Like said, I couldn’t come up at all with any examples for the use of such data type. So maybe such a construct is overblown, and having two specific conversion methods for enums is pragmatic enough.