slovo/docs/language/examples/supported/array-struct-elements.slo
2026-05-22 08:38:43 +02:00

42 lines
1.0 KiB
Plaintext

(module main)
(enum Color Red Blue Green)
(struct Pixel
(x i32)
(label string)
(color Color))
(fn make_pixels ((head string)) -> (array Pixel 3)
(array Pixel (Pixel (x 1) (label head) (color (Color.Red))) (Pixel (x 2) (label "mid") (color (Color.Blue))) (Pixel (x 3) (label "tail") (color (Color.Green)))))
(fn echo_pixels ((pixels (array Pixel 3))) -> (array Pixel 3)
pixels)
(fn at ((pixels (array Pixel 3)) (i i32)) -> Pixel
(index pixels i))
(fn first_label ((head string)) -> string
(. (at (make_pixels head) 0) label))
(fn last_color ((head string)) -> Color
(. (index (echo_pixels (make_pixels head)) 2) color))
(test "struct array string field access"
(= (first_label "head") "head"))
(test "struct array nested enum field access"
(= (last_color "head") (Color.Green)))
(test "struct array local param return call flow"
(= (. (at (echo_pixels (make_pixels "sun")) 1) x) 2))
(fn main () -> i32
(match (last_color "head")
((Color.Green)
0)
((Color.Red)
1)
((Color.Blue)
1)))