flan/test/programs/enum-compare.flan
Joseph Ferano 9a820d86cd Sweep every field label from the colon spelling to the dot
The script is in tools/ rather than thrown away, because two lanes are
writing Flan in the old spelling right now and their files need the same
pass at merge.

It works on forms, not on text: a keyword becomes a dot only where it sits
in a field-label position inside a brace, so an enum member in value
position, a map key inside an EDN string and a type-position {K V} are all
left alone. :keys keeps its colon -- it names no field.
2026-09-12 14:47:54 +07:00

28 lines
1.0 KiB
Plaintext

;;;; Comparing enums, which type checked and then died in the backend.
;;;;
;;;; Types.is_comparable admits an enum, so the checker was stating an intent
;;;; emit never honoured: (= k :a) passed check and then raised
;;;; Failure("comparison on K") with no source location. An enum is an i32 at
;;;; run time, so all six operators lower to icmp.
(defenum K [lo -1 mid 0 hi 1])
(defstruct S [k K])
(defn eq? [k K] bool (= k :mid))
(defn below? [k K] bool (< k :mid))
(defn main [] i32
;; Equality, both ways round.
(println (if (eq? :mid) "eq yes" "eq no"))
(println (if (eq? :hi) "eq yes" "eq no"))
;; Ordering, and signed: lo is -1, so an unsigned compare would call it the
;; largest member and answer the other way.
(println (if (below? :lo) "lo below mid" "lo not below mid"))
(println (if (below? :hi) "hi below mid" "hi not below mid"))
;; And through a struct field, which is a different path to the same compare.
(let [s (S {.k :hi})]
(println (if (= (.k s) :hi) "field eq yes" "field eq no")))
0)