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

57 lines
1.6 KiB
Plaintext

(module main)
(struct ArrayRecord
(ints (array i32 3))
(wides (array i64 2))
(ratios (array f64 3))
(flags (array bool 3))
(words (array string 3)))
(fn make_record ((base i32) (head string)) -> ArrayRecord
(ArrayRecord (ints (array i32 base (+ base 1) (+ base 2))) (wides (array i64 40i64 41i64)) (ratios (array f64 1.5 2.5 3.5)) (flags (array bool false true true)) (words (array string head "moon" "star"))))
(fn echo_record ((record ArrayRecord)) -> ArrayRecord
record)
(fn local_record ((head string)) -> ArrayRecord
(let record ArrayRecord (make_record 7 head))
(echo_record record))
(fn int_at ((record ArrayRecord) (i i32)) -> i32
(index (. record ints) i))
(fn wide_at ((record ArrayRecord) (i i32)) -> i64
(index (. record wides) i))
(fn ratio_at ((record ArrayRecord) (i i32)) -> f64
(index (. record ratios) i))
(fn flag_at ((record ArrayRecord) (i i32)) -> bool
(index (. record flags) i))
(fn word_at ((record ArrayRecord) (i i32)) -> string
(index (. record words) i))
(test "struct array i32 field access"
(= (int_at (make_record 7 "sun") 2) 9))
(test "struct array i64 field access"
(= (wide_at (make_record 7 "sun") 1) 41i64))
(test "struct array f64 field access"
(= (ratio_at (make_record 7 "sun") 2) 3.5))
(test "struct array bool field access"
(flag_at (make_record 7 "sun") 1))
(test "struct array string field access"
(= (word_at (make_record 7 "sun") 1) "moon"))
(test "struct array local param return call flow"
(= (word_at (local_record "sun") 0) "sun"))
(fn main () -> i32
(if (flag_at (local_record "sun") 2)
0
1))