Open dan-zheng opened 3 years ago
[Again a compiler-y example but] One example of a library using GADTs to ensure invariants is the Haskell
Hoopl library (paper, package docs), where GADTs can be used to ensure the right connectivity. To get a quick overview, check out section 3 in the paper, particularly subsections 3.1 and 3.2.
I've used this library in the past, and having type-level guarantees for well-formedness was helpful in avoiding accidentally optimizing code into other code with illegal control flow.
That said, I think the usefulness of GADTs in the style of Hoopl would be a bit less in Swift because Swift doesn't have higher-rank types, and you sometimes want the ability to write rank-2 functions to handle different cases in a GADT uniformly.
Additional Detail from JIRA
| | | |------------------|-----------------| |Votes | 1 | |Component/s | Compiler | |Labels | New Feature | |Assignee | None | |Priority | Medium | md5: d2fd52e3d3d765d906a4ad16c648e2f9Issue Description:
Motivation
Generalized enums in Swift would be an implementation of generalized algebraic data types (GADTs). They enable enums with cases whose types are more refined, leading to more safety, user-exposed information, and usability - as opposed to using unsafe casts.
See this discussion thread on implementing GADTs: https://forums.swift.org/t/generalized-abstract-data-types-gadts/42298
See this reply for rationale for adding GADTs to Swift: https://forums.swift.org/t/generalized-abstract-data-types-gadts/42298/24
One part of this work will be to write a Swift Evolution pitch. The latest syntax for generalized enums:
Motivating use cases
Example from apple/swift-driver: https://forums.swift.org/t/generalized-abstract-data-types-gadts/42298/47