Crell / enum-comparison

A comparison of enumerations and similar features in different languages
80 stars 7 forks source link

UnitEnum definition #66

Closed iluuu1994 closed 3 years ago

iluuu1994 commented 3 years ago

The RFC specifies:

This type of enum (with just enum cases and no related data) is known as a “Unit Enum”.

Since ScalarEnums are also UnitEnums this definition is incorrect. We don't have a term for non-scalar enums at the moment.

Crell commented 3 years ago

Oh bat gizzards. That was written before ScalarEnum extended UnitEnum. We should unify the terminology one way or the other. What do we call non-scalar enums then if not unit enums? Or is unit enum still the correct term?

TysonAndre commented 3 years ago

AbstractEnum being extended by UnitEnum and ScalarEnum is another option, if support for more immutable data types such as Distance::Miles(10) are added (Enum was your original proposal; that conflicted with the token)

The is_enum() function could alternately be reintroduced?

https://github.com/Crell/enum-comparison/issues/43 is related

Crell commented 3 years ago

Thinking on the Naming Things(tm) problem, there's two levels to consider.

The cases:

The enum types:

cases() makes sense on Unit Enum and Scalar Enum, but not on Tagged Enum, because tagged cases cannot be enumerated. So... whatever interfaces Tagged Enum ends up with cannot include cases().

Since scalar cases MAY extend in the future to non-scalars (although I don't quite grok that), maybe "Backed Enum"?

Breaking it up into tiny little bits:

interface NamedEnum {
  public string $name;
}

interface EnumerableEnum {
  public function cases();
}

interface BackedEnum {
  public string $value;
  public function from();
  public function tryFrom();
}

// Today Unit Enum, can still use that name.
class Suit implements NamedEnum, EnumerableEnum {}

// Today Scalar Enum.  We could call it Backed Enum, I guess?
class Size implements NamedEnum, EnumerableEnum, BackedEnum {}

// Future tagged cases
class Maybe implements NamedEnum {}

Thoughts?

iluuu1994 commented 3 years ago

Hm, I think our current terminology is mostly fine, except for ScalarEnum. BackedEnum is fine by me although I'd still prefer ValueEnum. If we call ADTs ADTs or tagged unions there should be no confusion. I don't think we actually need a NamedEnum interface or an interface for ADTs (they have no special methods).

Crell commented 3 years ago

Well, one thing that the above breakdown demonstrated to me is that perhaps the ScalarEnum interface should not extend UnitEnum. Or rather, UnitEnum isn't a thing; An enum is enumerable or not. cases() won't appear on ADTs/tagged enums. NamedEnum doesn't need to be a thing because there's no methods on it, but I think EnumerableEnum and BackedEnum/ValueEnum/whatever make more sense, especially when looking forward to Tagged Enums.

iluuu1994 commented 3 years ago

Coming back to the original issue: How about changing the sentence to something like:

An enum that has a finite set of values enum is known as a “Unit Enum”. All of the enums proposed in this RFC are Unit Enums but this would change if algebraic data types are accepted into the language.