Skip to content

Commit be658d7

Browse files
authored
RFC #71: EnumView.Matches
2 parents ba991f4 + a96afbb commit be658d7

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

text/0071-enumview-matches.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
- Start Date: 2024-07-29
2+
- RFC PR: [amaranth-lang/rfcs#71](https://github.com/amaranth-lang/rfcs/pull/71)
3+
- Amaranth Issue: [amaranth-lang/amaranth#1468](https://github.com/amaranth-lang/amaranth/issues/1468)
4+
5+
# `EnumView.matches`
6+
7+
## Summary
8+
[summary]: #summary
9+
10+
A `matches` method is added to `EnumView`, performing a similar function to `Value.matches`.
11+
12+
## Motivation
13+
[motivation]: #motivation
14+
15+
Amaranth currently has `Value.matches`, which checks whether a value matches any of the given patterns. An obvious application of such a feature is using it with an enumerated value. However, as it stands, `EnumView` doesn't provide a corresponding method natively, requiring a cast to `Value`. Additionally, casting the `EnumView` to `Value` erases the enumeration type, providing no protection against matching with values from the wrong type.
16+
17+
This RFC proposes to fill that gap by adding a type-safe `EnumView.matches` method.
18+
19+
## Guide-level explanation
20+
[guide-level-explanation]: #guide-level-explanation
21+
22+
The `EnumView.matches` method can be used to check whether an enumerated value belongs to a given set:
23+
24+
```
25+
class Color(enum.Enum, shape=3):
26+
BLACK = 0
27+
RED = 1
28+
GREEN = 2
29+
BLUE = 4
30+
WHITE = 7
31+
32+
s = Signal(Color)
33+
m.d.comb += is_grayscale.eq(s.matches(Color.BLACK, Color.WHITE))
34+
```
35+
36+
## Reference-level explanation
37+
[reference-level-explanation]: #reference-level-explanation
38+
39+
The following method is added:
40+
41+
- `EnumView.matches(*values)`: equivalent to `self.as_value().matches(*values)`, but requires all `values` to be a member of the same enum as the `EnumView`, raising a `TypeError` otherwise
42+
43+
## Drawbacks
44+
[drawbacks]: #drawbacks
45+
46+
- more moving parts in the language
47+
- more general pattern matching facilities have been requested on multiple occasions, which this facility could end up redundant with
48+
49+
## Rationale and alternatives
50+
[rationale-and-alternatives]: #rationale-and-alternatives
51+
52+
The proposed functionality is an obvious extension of `Value.matches` to the `EnumView` class. The proposed typechecking is consistent with current `EnumView` behavior.
53+
54+
## Prior art
55+
[prior-art]: #prior-art
56+
57+
`Value.matches`, Rust `matches!()`.
58+
59+
## Unresolved questions
60+
[unresolved-questions]: #unresolved-questions
61+
62+
None.
63+
64+
## Future possibilities
65+
[future-possibilities]: #future-possibilities
66+
67+
None for `EnumView`. However, there have been discussions of having more advanced pattern matching facilities in the language in general.

0 commit comments

Comments
 (0)