Skip to content

choice and match for structs #1

Description

@ebinwalter

Motivation

I can't help but love Rust's enums. However, sometimes I wish I could add a parameter to every variant of an enum at once, and so I end up with something like

struct Type {
  common_value: i32,
  ty: TypeKind,
}

enum TypeKind {
  Integer,
  Ref(Rc<Type>),
  Array(Rc<Type>, u32),
}

Proposal

I would like to add something like the following to our language.

struct Type {
  choice Integer;
  choice Ref(struct Type *);
  choice Array {
    struct Type *ty;
    int length;
  }
  
  int common_value;
}

int x = match type {
  Integer -> {
    0 + type.common_value
  }
  Ref(t) -> {
    1 + type.common_value
  }
  Array a {
    a.length + type.common_value
  }
};

What would this give us? We could avoid if-else chains with tags on structs, and we could essentially get the Rust-style enum for free by creating a struct with only choices.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions