28624, "jabraham17", "Should unions throw or halt?", "2026-03-31T21:31:33Z"
In Expand and improve support for union by jabraham17 · Pull Request #28601 · chapel-lang/chapel · GitHub, I made quite a few improvements to unions. One of those is making the halts turned off via --no-checks. However, it might be really nice to have these errors throw instead
For example
union u {
var x: int;
var y: real;
}
var u: U;
u.x = 1;
u.y; // throws, y is not active
However, this can have other implications. For instance, now every context where a union is accessed is a throwing context, which does not sound like a good idea.
A few possible solutions
- Introduce a
tryAccess, similar to:vstryCast - make the field access always throw, but add some special syntax for exceptions
- expand the select syntax to introduce captures to acomplish the field access and check in a single way. TODO SEE ISSUE
u!.xuses the halting path (turned off with--no-checks) to get the previous behavior with no throwing overhead when you know the the union is active for that field.- this feels appealing, but implies other issues. For example, is
var y = u!;legal? Whats its type? Does this imply!.becomes special syntax?
- this feels appealing, but implies other issues. For example, is