Skip to content

Commit 37cf50c

Browse files
hua7450claude
andcommitted
Add Enum dispatch pattern for select() — list all conditions explicitly
When using select() with Enum variables, list all values explicitly and set default to match the Enum's default_value for consistency. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 7f16cb6 commit 37cf50c

File tree

1 file changed

+31
-0
lines changed
  • skills/technical-patterns/policyengine-vectorization-skill

1 file changed

+31
-0
lines changed

skills/technical-patterns/policyengine-vectorization-skill/SKILL.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,37 @@ else:
7070
)
7171
```
7272

73+
### Pattern 2b: Enum Dispatch → List ALL Conditions
74+
75+
When dispatching on an Enum variable, list all values explicitly in `select()`. Set the `default` to match the Enum's `default_value` so unexpected values get consistent behavior.
76+
77+
```python
78+
# ❌ BAD — hides one option in default, unclear which is the fallback
79+
provider_type = person("ri_ccap_provider_type", period)
80+
types = provider_type.possible_values
81+
return select(
82+
[
83+
provider_type == types.LICENSED_CENTER,
84+
provider_type == types.LICENSED_FAMILY,
85+
],
86+
[center_rate, family_rate],
87+
default=exempt_rate, # Why exempt as fallback?
88+
)
89+
90+
# ✅ GOOD — all options listed, default matches Enum default_value
91+
provider_type = person("ri_ccap_provider_type", period)
92+
types = provider_type.possible_values
93+
return select(
94+
[
95+
provider_type == types.LICENSED_CENTER,
96+
provider_type == types.LICENSED_FAMILY,
97+
provider_type == types.LICENSE_EXEMPT,
98+
],
99+
[center_rate, family_rate, exempt_rate],
100+
default=center_rate, # Matches default_value = LICENSED_CENTER
101+
)
102+
```
103+
73104
### Pattern 3: Boolean Operations
74105

75106
```python

0 commit comments

Comments
 (0)