-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstruct_enum.ecl
More file actions
54 lines (45 loc) · 1.15 KB
/
struct_enum.ecl
File metadata and controls
54 lines (45 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// SPDX-License-Identifier: PMPL-1.0-or-later
// Struct and enum definitions in Eclexia
type Point = { x: Float, y: Float }
type Color = enum {
Red,
Green,
Blue,
}
fn color_name(c: Color) -> String {
match c {
Red => "red",
Green => "green",
Blue => "blue",
}
}
fn midpoint(ax: Float, ay: Float, bx: Float, by: Float) -> Point {
Point {
x: (ax + bx) / 2.0,
y: (ay + by) / 2.0,
}
}
fn main() {
println("=== Struct & Enum Demo ===")
// Struct creation and field access
let origin = Point { x: 0.0, y: 0.0 }
let p = Point { x: 3.0, y: 4.0 }
println("Origin:", origin)
println("Point p:", p)
println("p.x =", p.x)
println("p.y =", p.y)
// Computed struct
let mid = midpoint(0.0, 0.0, 6.0, 8.0)
println("Midpoint:", mid)
// Enum variants
println("")
println("Color Red:", color_name(Red))
println("Color Green:", color_name(Green))
println("Color Blue:", color_name(Blue))
// Struct with different values
let q = Point { x: -1.5, y: 2.7 }
println("")
println("Point q:", q)
println("q.x =", q.x)
println("q.y =", q.y)
}