Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enum abstract instances #86

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Enum abstract instances #86

wants to merge 2 commits into from

Conversation

markknol
Copy link
Member

@markknol markknol commented Nov 24, 2020

This proposal would allow instances as enum abstract values (generated as static singletons).
Currently this is not possible (error: Inline variable initialization must be a constant value)

Rendered version

@RealyUniqueName
Copy link
Member

Won't this make our ordinary enums obsolete?

@RealyUniqueName
Copy link
Member

I guess it's one more step to replace ordinary enums.
The step is to make enum abstracts parameterized :)

enum abstract Person( { final name:String; final age:Int; } )  {
  final Mark = { name: "Mark", age: 21 };
  final John = { name: "John", age: 25 };
  final Elisa = { name: "Elisa", age: 28 };
  final Fifties(name:String) = { name:name, age:50 }
}
//...
switch person {
  case Fifties('Herman'): trace('Herman is 50 years old');
  case _:
}

@grepsuzette
Copy link

grepsuzette commented May 31, 2021

Some things are a bit confusing for me.
Something from your example:

enum abstract Person({ final name: String; final age:Int }) {
  final John = { name: "John Bon", age: 65 };
  final Jane = { name: "Jane Dane", age: 18 };
  final Mark = { name: "Mark Hark", age: 36 };
  final Elisa = { name: "Elisa Disa", age: 42 };
}

That would mean a from { final name:String, final age:Int } clause is forbidden, right?

Another question (it was tipped in your final questions):
Are non-final fields forbidden? For example:

enum abstract Person({ name: String, age:Int }) {
  final John = { name: "John Bon", age: 65 };
  final Jane = { name: "Jane Dane", age: 18 };
}

var p = John;
p.name = "Jane Dane";
p.age = 18;
switch p {
  case John:
  case Jane:
// Is it Jane or John? And if you set the age to 17?
}

@Simn Simn added this to the 2021-12 milestone Nov 8, 2021
@Simn
Copy link
Member

Simn commented Nov 9, 2021

We didn't reach a conclusion on this proposal in the haxe-evolution meeting today.

There are some open questions here related to the identity and equality of such values. We agreed to postpone the decision here and discuss this at a later point.

@Simn Simn removed this from the 2021-12 milestone Nov 9, 2021
@EliteMasterEric
Copy link

EliteMasterEric commented Jan 7, 2024

Looking at this proposal, it seems that the core issue here is the inability to associate a single enumerated value with several data points. The syntax provided however seems complicated and confusing for this use case.

It may make more sense to extend the functionality of the bare enum type instead, to allow creating enum constructors which contain constant values.

// The syntax for this probably needs a second opinion or refinement
enum Color(Int, Int, Int) {
  Rgb(r:Int, g:Int, b:Int);
  Red(255, 0, 0);
  Green(0, 255, 0);
  Blue(0, 0, 255);
  Cyan(0, 255, 255);
  Magenta(255, 0, 255);
  Yellow(255, 255, 0);
  DarkRed(r:Int, 0, 0);
}

class Main {
  static function main() {
    var color = getColor();
    switch (color) {
      case Red(r, g, b):
        trace("Color was red, full intensity " + r);
      case DarkRed(r, g, b):
        trace("Color was red, intensity " + r);
      case Rgb(r, g, b):
        trace("Color had a red value of " + r);
    }
  }
}

Although I'm not sure whether this is better than just creating a static extension on the enum and storing the data in a switch/case structure on that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants