mirror of
https://github.com/Nexoniarz/Finch.git
synced 2026-11-03 00:44:27 +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
12
tests/new/fail/payload_missing.fch
Normal file
12
tests/new/fail/payload_missing.fch
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
// expect: this match doesn't handle Point
|
||||
enum Shape {
|
||||
Circle(float)
|
||||
Point
|
||||
}
|
||||
|
||||
fn main() {
|
||||
s := Shape.Circle(1.0)
|
||||
match s {
|
||||
Circle(r) -> print(r)
|
||||
}
|
||||
}
|
||||
38
tests/new/payload_enums.fch
Normal file
38
tests/new/payload_enums.fch
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
enum Shape {
|
||||
Circle(float)
|
||||
Rect(float, float)
|
||||
Named(str, []int)
|
||||
Point
|
||||
}
|
||||
|
||||
fn Shape.area() -> float {
|
||||
return match self {
|
||||
Circle(r) -> 3.0 * r * r
|
||||
Rect(w, h) -> w * h
|
||||
Named(_, sizes) -> float(sizes.len)
|
||||
Point -> 0.0
|
||||
}
|
||||
}
|
||||
|
||||
fn describe(Shape s) -> str {
|
||||
match s {
|
||||
Shape.Circle(r) -> return "circle " + str(r)
|
||||
Shape.Named(name, _) -> return "named " + name
|
||||
else -> return "other"
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
shapes := [Shape.Circle(2.0), Shape.Rect(2.0, 3.5), Shape.Named("tri", [1, 2, 3]), Shape.Point]
|
||||
for s in shapes {
|
||||
print(s, s.area(), describe(s))
|
||||
}
|
||||
copy := shapes[2]
|
||||
shapes.clear()
|
||||
print(copy)
|
||||
p := Shape.Point
|
||||
match p {
|
||||
Point -> print("a point")
|
||||
else -> print("no")
|
||||
}
|
||||
}
|
||||
6
tests/new/payload_enums.out
Normal file
6
tests/new/payload_enums.out
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
Circle(2) 12 circle 2
|
||||
Rect(2, 3.5) 7 other
|
||||
Named("tri", [1, 2, 3]) 3 named tri
|
||||
Point 0 other
|
||||
Named("tri", [1, 2, 3])
|
||||
a point
|
||||
Loading…
Reference in a new issue