mirror of
https://github.com/Nexoniarz/Finch.git
synced 2026-11-03 08:54:25 +00:00
Enums whose variants hold values
enum Shape { Circle(float), Rect(float, float), Point }; Shape.Circle(2.0) makes one, match names the values
(Circle(r), _ to ignore), exhaustive like plain enums, printing shows Rect(2, 3.5). Underneath it is a struct (tag +
every variant's values), so copying, freeing, methods and valgrind-cleanliness come with it. Also: stray note files removed.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015f73FesGZGYPkmDvUcj6QX
This commit is contained in:
parent
dc0334fa0e
commit
3e45b62812
13 changed files with 75170 additions and 66737 deletions
|
|
@ -428,4 +428,4 @@ test (22 checks).
|
|||
- The driver uses `sh` utilities (`rm`, `mkdir`), so it needs a POSIX shell.
|
||||
- Generics are monomorphised (code size grows with instantiations); there are no trait bounds, a wrong use is an error
|
||||
inside the instantiated body.
|
||||
- No payload enums yet (variants carry no data).
|
||||
|
||||
|
|
|
|||
|
|
@ -924,6 +924,29 @@ tests/cross.sh windows # every test built for Windows and run with Wine
|
|||
tests/cross.sh arm64 # every test built for ARM64 Linux and run with QEMU
|
||||
```
|
||||
|
||||
### Variants that hold values
|
||||
|
||||
A variant can carry values. Make one with `Shape.Circle(2.0)`, take them out with `match`, which names them:
|
||||
|
||||
```c
|
||||
enum Shape {
|
||||
Circle(float)
|
||||
Rect(float, float)
|
||||
Point
|
||||
}
|
||||
|
||||
fn Shape.area() -> float {
|
||||
return match self {
|
||||
Circle(r) -> 3.14 * r * r
|
||||
Rect(w, h) -> w * h
|
||||
Point -> 0.0
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
`_` ignores a value. `match` must handle every variant (or end with `else`). Printing shows `Rect(2, 3.5)`. Such an enum is
|
||||
copied and freed like a struct; it can't be compared with `==` (match on it). Variants of these enums can't have numbers.
|
||||
|
||||
---
|
||||
|
||||
## 23. Enums and match
|
||||
|
|
@ -1027,7 +1050,7 @@ write_file(path.join("out", "config.json"), json.pretty(doc))
|
|||
|
||||
Not there yet:
|
||||
|
||||
- Interfaces, and enums whose variants carry values (`Circle(radius)`).
|
||||
- Interfaces.
|
||||
- Unicode-aware text: `.len`, `s[i]` and `upper()` work on bytes / ASCII.
|
||||
- Threads.
|
||||
- Windows on ARM64, 32-bit systems, WebAssembly.
|
||||
|
|
|
|||
Loading…
Reference in a new issue