Repository navigation
case: type switch with T! patterns (Dang 2.4) - #286
Merged
Merged
Conversation
A type pattern is now written as the non-null type its arm binds (`c: Cat! =>`) and may be any type: named types match by identity as before, lists and maps by their element types, records by their fields. A pattern must overlap the operand's type, and unreachable-arm and exhaustiveness checks cover the new kinds, so `[Int!]! | [String!]!` can be covered without an else. Bare patterns parse before Dang 2.4 (new Version2_4, now CurrentVersion) and migrate by adding the `!`; in 2.4 they are an error with a hint. The tracked test, demo and LSP fixture projects move to 2.4, migrated with dang fmt. Lists, maps and selection results now record their static element type as ElemType, instead of the first element's runtime type, and an arm matches by that type. Where it still holds type variables the elements decide, and an exhaustive case falls back to the contents rather than failing. Signed-off-by: Alex Suraci <[email protected]>
The guide, error pages and skill references write every type pattern with its `!`, describe case as a type switch over lists, maps and records, and explain that a list matches by the element type it was built with, not by what it holds. Example dang.toml files show 2.4. Signed-off-by: Alex Suraci <[email protected]>
- Formatting keeps a type pattern's `!` as written where it is optional (before 2.4): the pattern records it (PatternBang), a spelling-only field that migration's AST comparison ignores. Formatting under 2.1 used to drop the `!` from `[Decoded!]!`, which kept the meaning but not the spelling. - A list or map passing a generic parameter keeps the element type it recorded, instead of recording the type variable; only a concrete target re-records it. - `+` and `+=` on lists record the static type of the result, not a join of the operands' recorded types. - A list pattern on a GraphQL object list, which is a lazy handle until its fields are selected, is a compile error with a hint, instead of type-checking and never matching. - An ID pattern matches an ID, which is a string at runtime. Signed-off-by: Alex Suraci <[email protected]>
Deploying dang with
|
| Latest commit: |
97a94d6
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://49502beb.dang-3kk.pages.dev |
| Branch Preview URL: | https://case-type-switch.dang-3kk.pages.dev |
vito
force-pushed
the
case-type-switch
branch
from
November 9, 2026 21:53
158eca7 to
2cd715f
Compare
vito
force-pushed
the
case-type-switch
branch
from
November 9, 2026 22:05
2cd715f to
be35fe9
Compare
vito
force-pushed
the
case-type-switch
branch
from
November 9, 2026 22:15
be35fe9 to
56e8e61
Compare
vito
force-pushed
the
case-type-switch
branch
from
November 9, 2026 22:36
56e8e61 to
709dece
Compare
vito
force-pushed
the
case-type-switch
branch
from
November 9, 2026 22:50
709dece to
27f4f4c
Compare
An interface pattern on decoded data now points at the types implementing it; "match its member types" stays for unions, which get their own error test. Signed-off-by: Alex Suraci <[email protected]>
The skill references no longer describe the removed decoded-only rule for list and map patterns. They say which types a union or a plain object operand accepts as patterns, and that a generic parameter keeps a list's recorded element type. Signed-off-by: Alex Suraci <[email protected]>
- A named type pattern is spelled as a Symbol again, so a type named in lowercase (as GraphQL schemas may name them) can be matched; other patterns go through the Type grammar. - A union narrowed by inline fragments takes only its members as patterns: an interface or union a member belongs to would bind a value holding only the selected fields as a type with others. - An ID pattern takes a plain string (a GraphQL ID result) only where the operand can't be a String, so `String! | ID!` arms aren't decided by order; matchesType no longer treats strings as IDs. Signed-off-by: Alex Suraci <[email protected]>
Say that a pattern must relate to the operand by identity, implementation or union membership, so two unrelated interfaces don't overlap even when one type implements both. Signed-off-by: Alex Suraci <[email protected]>
- Map.with and Map.merge record a value type both sides fit, so a map that gains a value of another type no longer matches its old one. - An ID pattern takes plain strings anywhere in the pattern (a list of IDs built in a generic body, say) when the operand's type holds no String, through a matchMode passed to valueInhabits. - A type arm after one that takes every non-null value of the operand is an unreachable-clause error. Signed-off-by: Alex Suraci <[email protected]>
The skill references said the `!` was required "since Dang 2.4" and how to migrate to it; the dang.toml reference already covers older projects. The guide called a total pattern "fine", answering the old union-or-interface rule, and its list of types a pattern may name left out a union with a qualifying member. A rescue pattern can be any error type, a union of Error implementers included, and the grammar notes still sketched a rescue clause as a bare type name. Signed-off-by: Alex Suraci <[email protected]>
The analysis tests from main analyze source as the current version, which requires a type pattern's `!`. Signed-off-by: Alex Suraci <[email protected]>
A rescue arm only binds raised errors, but a union pattern such as `E1 | Foo` passed the overlap check on Error! through its error member alone. Every option or member of a rescue pattern (nested unions included) must now overlap Error, or the clause is a compile error naming the member that is never raised. Signed-off-by: Alex Suraci <[email protected]>
vito
force-pushed
the
case-type-switch
branch
from
November 9, 2026 23:00
27f4f4c to
53cd9e1
Compare
The union-member check is skipped for try/catch blocks, which accept patterns that can never match as dead code in every version, so legacy code with such a union pattern keeps compiling. The skill documents the new rescue error. Signed-off-by: Alex Suraci <[email protected]>
vito
force-pushed
the
case-type-switch
branch
from
November 10, 2026 03:07
3f4d343 to
97a94d6
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
caseis now a type switch. Every type pattern is written as the non-null type its arm binds (c: Cat! =>), and a pattern can be any type: a named type, a list, a map, or a record.This bumps the language to Dang 2.4 (
Version2_4, nowCurrentVersion). PRs that change the language and land after this one should reuse 2.4 rather than add another version.Merge order
Based on main; #291 and #292 stack on top.
What changes
Q-46:
!is required on type patternscaseon unions, interfaces and anything else, torescue, and to decoded arms.binding: TypePattern =>.Symbol, optionally qualified, so lowercase type names (common in GraphQL schemas) still match.Typegrammar.case.!, which every pattern has.typePatternClausein version.go builds it and records two flags:BarePattern: no!was written where one is now required.PatternBang: a!was written. This field is spelling only; it carries thedang:"spelling"tag, whichcompareASTnow skips.barePatterns()), a bare pattern means the same as the!form, and the formatter prints each pattern as written.a type pattern is the non-null type its arm binds: write `c: Cat! =>`.dang migrateanddang fmt -wadd the!. A source already written with!everywhere migrates unchanged.try/catchkeeps its bare-name clauses.tests/,tests/importcalc/,demos/github/andpkg/lsp/testdata/.tests/importcalcdeclares its own version, so it was bumped explicitly. Their sources were reformatted withdang fmtunder 2.4, which adds the!. I couldn't rundang migrateitself from my environment, butMigrateSourceis covered byTestVersionTypePatternsand the new migration fixture.tests/testdata/migrations/2.1-to-2.4fixture covers union, interface, decoded, list and map patterns, rescue clauses, try/catch and patterns already written with!.DefaultVersion, so adang.tomlwithout adangkey followsCurrentVersionlike source nodang.tomlgoverns. Once both land, keyless projects are 2.4 and need the!; to keep bare patterns they pindang = "2.1"(or migrate withdang migrate --from 2.1).Q-47:
caseis a general type switchtypesOverlap). For named types, overlap means one is a subtype of the other, or they share a union member. It is conservative: two unrelated interfaces don't overlap, even if one type implements both.pattern P can't match a value of type O.not a member of union/does not implement interfaceerrors stay.valueInhabits, newtype_switch.go):Decodedkeeps case: decoded-data and qualified type patterns #285's shape matching.Decodedis still a compile error.typeCoveredBy) covers inline unions of any pattern kind, so[Int!]! | [String!]!needs no else.typeFits) cover the new kinds. That includes[Cat!]!after[Animal!]!, and any arm after one that takes every value of the operand.nullarm orelseto be exhaustive.ID!pattern takes such strings only where the operand's type holds noString, soString! | ID!arms aren't decided by order.Q-48 option B: static element types
+and+=on lists,map/flatMap, andMap.with/mergenow record their static element type as the runtimeElemType. Before, a list recorded its first element's runtime type, and selection lists recorded nothing.[a]!parameter keeps what the list recorded.ElemTypereaders: on this base they are matching, the stdlib's propagation (filter/sort/take…, which keep subsets) andListValue.Type(). Display and encoding read elements, notElemType.Release note
!. Acase,rescueor decoded-data pattern is written as the non-null type its arm binds:c: Cat! =>,e: Error! =>,items: [Decoded!]! =>,g: Lib.Greeter! =>.dang = "2.1"(or 2.2/2.3) keep working unchanged. Rundang migrateto move to 2.4, which adds the!.dang initnow writesdang = "2.4". Source that nodang.tomlgoverns is read as 2.4, so bare patterns there are errors with a hint.dang.tomlwithout adangkey follows the running Dang (version: a dang.toml without a version is current #287), so it is read as 2.4 too: pindang = "2.1"to keep it, or move it withdang migrate --from 2.1.caseis a type switch.[Animal!]!doesn't match[Cat!]!even if every element is a Cat.Testing
**) pass at the head after rebasing onto main, including docs, fmt/stale, generate/stale, tree-sitter and editor highlights. On the rebase, empty literals keep dang: empty collections carry their inferred type #278's resolved element type, and non-empty literals, materialized boundaries andmapresults record their static element type resolved through dang: empty collections carry their inferred type #278's runtime type arguments.tests/test_case_type_switch.dang: named, supertype and union patterns; nullable operands; list, map and record switches; recorded vs. built element types; the generic fallback and generic boundaries; concatenation;Map.with/merge; lowercase type names;String! | ID!.test_graphql_abstract_case.dang:case_bare_type_patterncase_type_pattern_disjointcase_list_pattern_disjointcase_nullable_operand_not_exhaustivecase_unreachable_list_armcase_unreachable_after_total_armcase_type_pattern_type_variablecase_graphql_object_listcase_narrowed_union_interfaceTestVersionTypePatterns:!.versionsnow includes 2.4.TestKeywordNamesRoundTripalso covers type-pattern bindings.!.caseon a mutation-reached union with an interface pattern every member implements still raises at runtime. That is consistent with the existing design: mutation handles are never resolved, and patterns covering every member already behave the same way.m: Map[Int!]! =>parses with an ERROR node, because the lexer readsMapas a word token. Highlighting only.Open
CommonSupertypepicks arbitrarily between an interface and a union that both contain two types (e.g.[Cat(), Dog()]withAnimalandPet). Which list arm matches such a literal follows that pre-existing choice.