Skip to content

Commit

Permalink
Add the alternative suggested by @oli-obk
Browse files Browse the repository at this point in the history
  • Loading branch information
nox committed Mar 17, 2018
1 parent e7fd9c4 commit 93f3170
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions text/0000-arbitrary_enum_discriminant.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,12 @@ This introduces one more knob to the representation of enumerations.

Reusing the current syntax and semantics for explicit discriminants of
field-less enumerations means that the changes to the grammar and semantics of
the language are minimal. The alternative is to put the specified discriminants
in variant attributes, but this would be at odds with the syntax for field-less
enumerations.
the language are minimal. There are a few possible alternatives nonetheless.

## Discriminant values in attributes

We could specify the discriminant values in variant attributes, but this would
be at odds with the syntax for field-less enumerations.

```rust
enum ParisianSandwichIngredient {
Expand All @@ -246,6 +249,30 @@ enum ParisianSandwichIngredient {
}
```

## Use discriminants of a separate field-less enumeration

We could tell rustc to tie the discriminants of the enumeration to the
variants of a separate field-less enumeration.

```rust
#[discriminant(IngredientKind)]
enum ParisianSandwichIngredient {
Bread(BreadKind),
Ham(HamKind),
Butter(ButterKind),
}

enum IngredientKind {
Bread,
Ham,
Butter,
}
```

This isn't applicable if such a separate field-less enumeration doesn't exist,
and this can easily be done as a procedural macro using the feature described
in this RFC. It also looks way more like spooky action at a distance.

# Prior art
[prior-art]: #prior-art

Expand Down

0 comments on commit 93f3170

Please sign in to comment.