Skip to content

[Enum] generic, constraint, types and named parameters for ADT Enums #2006

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

Closed
igotfr opened this issue Dec 2, 2021 · 7 comments
Closed

[Enum] generic, constraint, types and named parameters for ADT Enums #2006

igotfr opened this issue Dec 2, 2021 · 7 comments
Labels
feature Proposed language feature that solves one or more problems

Comments

@igotfr
Copy link

igotfr commented Dec 2, 2021

Dart enums are anemics and force us to use class

Dart Proposal

enum Option<T> {
    None,
    Some(T)
}

var functor = Option<int>.Some(4);
enum IpAddr {
    V4(String),
    V6(String)
}

var ip = IpAddr.V4("0.0.0.0");
enum IpAddr {
    V4(int, int, int, int),
    V6(String)
}

var ip = IpAddr.V4(192, 168, 1, 1);
enum Message {
    Quit,
    Move({ int x, int y }),
    Write(String),
    ChangeColor(int, int, int)
}

var message = Message.Move(x: 45, y: -82);

Rust Enums

pub enum Option<T> {
    None,
    Some(T)
}
enum IpAddr {
    V4(String),
    V6(String)
}
enum IpAddr {
    V4(u8, u8, u8, u8),
    V6(String)
}
enum Message {
    Quit,
    Move { x: i32, y: i32 },
    Write(String),
    ChangeColor(i32, i32, i32)
}
@igotfr igotfr added the feature Proposed language feature that solves one or more problems label Dec 2, 2021
@igotfr igotfr changed the title [Enum] generic, constraint, types and named parameters for Enums [Enum] generic, constraint, types and named parameters for ADT Enums Dec 2, 2021
@mateusfccp
Copy link
Contributor

Duplicate of #349.

@igotfr
Copy link
Author

igotfr commented Dec 2, 2021

relateds:
#158
#546

@igotfr
Copy link
Author

igotfr commented Dec 2, 2021

enum Foo {
    Bar,            // 0
    Baz = 123,      // 123
    Quux,           // 124
}

@Levi-Lesches
Copy link

I think this can be closed now that the enhanced-enums proposal is being worked on. Issue #158 is the official tracking issue for the topic.

@igotfr
Copy link
Author

igotfr commented Dec 2, 2021

I think this can be closed now that the enhanced-enums proposal is being worked on. Issue #158 is the official tracking issue for the topic.

I think this is very verbose and unnecessary, I've never seen it anywhere. The variations I know are Haskell's data, Rust's enum, and V's type (sumtype)

https://github.com/vlang/v/blob/master/doc/docs.md#sum-types

@Levi-Lesches
Copy link

I don't quite understand your concern. Looking at your examples, you seem to be asking for a way to give enums parameters and fields, like regular classes. I see that some of your examples don't take any arguments, or have a different signature than the other elements of the enum, but that's a smaller detail. The enhanced-enums proposal gives you the ability to treat enums like regular classes. Combine that with the idea of enum-like classes and you should be fine. Your first example would be something like:

/// Enum-like class -- works today.
class Option<T> {
  final int? value;
  const Option([this.value]);

  static const none = Option();
  static const functor = Option<int>(4);
}

/// Enhanced enum -- requires the enhanced-enum feature.
enum Option<T> {
  none(),
  functor<int>(4);

  final T value? 
  const Option([this.value]);
}

@munificent
Copy link
Member

I'm going to close this out because there are already existing feature requires for sum types, pattern matching, and members inside enum types. In the future, if you have a language feature request, please write a detailed explanation for what problem you would like the language to solve and how you think it should solve it. Copy/pasting snippets of code from other languages does not tell us very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Proposed language feature that solves one or more problems
Projects
None yet
Development

No branches or pull requests

4 participants