No supported expression-level way to read or compare a sum's tag #74

Open
opened 2026-11-03 16:54:45 +01:00 by alex-eg · 0 comments
Owner

match is currently the only sum-aware form, and it is a statement: it cannot be used as an initializer, an argument, or an if test (match is a statement, and has no value, see tests/encapsulation.scm). A simple tag check therefore has to go through the generated representation.

A sum compiles to a struct with a tag field and generated enum constants:

(sum shape
  ((circle ((r float)))
   (rect   ((w float) (h float)))
   (empty  ())))

(var s shape (shape/circle 1.0))

;; works, but only because the representation is visible:
(if (== (. s tag) shapeǂcircle)
    ...)

That has several problems:

  • tag is an implementation field, not documented syntax.
  • shapeǂcircle is a generated name; ǂ is an internal separator, not something users should have to type.
  • The check is easy to get wrong and would break if the representation changes.

It would be useful to have a supported expression-level way to read or compare a tag, for example:

(tag s)                    ; the tag value
(tag-is? s 'circle)        ; boolean
(== (tag s) 'circle)       ; if tags are exposed as symbols/keywords

or generated per-sum predicates such as (shape/circle? s).

Requirements:

  • usable in expression position, unlike match;
  • usable on values and through pointers (p : (* shape)), like .;
  • no ǂ or other generated names in user code;
  • works for pub/imported sums;
  • tested for value and pointer receivers, a wrong variant, and an imported sum.

match should remain the exhaustive destructuring form. Making match an expression is another possible direction, but this issue is about the smaller tag-only case.

`match` is currently the only sum-aware form, and it is a statement: it cannot be used as an initializer, an argument, or an `if` test (`match is a statement, and has no value`, see `tests/encapsulation.scm`). A simple tag check therefore has to go through the generated representation. A sum compiles to a struct with a `tag` field and generated enum constants: ```scheme (sum shape ((circle ((r float))) (rect ((w float) (h float))) (empty ()))) (var s shape (shape/circle 1.0)) ;; works, but only because the representation is visible: (if (== (. s tag) shapeǂcircle) ...) ``` That has several problems: - `tag` is an implementation field, not documented syntax. - `shapeǂcircle` is a generated name; `ǂ` is an internal separator, not something users should have to type. - The check is easy to get wrong and would break if the representation changes. It would be useful to have a supported expression-level way to read or compare a tag, for example: ```scheme (tag s) ; the tag value (tag-is? s 'circle) ; boolean (== (tag s) 'circle) ; if tags are exposed as symbols/keywords ``` or generated per-sum predicates such as `(shape/circle? s)`. Requirements: - usable in expression position, unlike `match`; - usable on values and through pointers (`p : (* shape)`), like `.`; - no `ǂ` or other generated names in user code; - works for `pub`/imported sums; - tested for value and pointer receivers, a wrong variant, and an imported sum. `match` should remain the exhaustive destructuring form. Making `match` an expression is another possible direction, but this issue is about the smaller tag-only case.
alex-eg added the feature label 2026-11-03 16:54:45 +01:00
Sign in to join this conversation.