New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SR-13955] Support computed cases (aka pattern synonyms) for ergonomic binding. #56352
Comments
@swift-ci create |
What I'm suggesting is the equivalent of computed properties but for cases. More specifically, you can specify a matcher (shown with Additionally, you can say that a group of computed cases are exhaustive together using If you're familiar with Haskell, what I'm suggesting here is roughly equivalent to GHC's PatternSynonyms extension, and I don't quite understand the semantics of what you're suggesting. A matcher needs to return values that can be bound. In your example, the matcher is returning true/false; I don't think that is sufficient information. For example, you've written: case let Contained(count: myCount, containedIn: myContainedIn): It is not clear what values |
Comment by Daniel Sweeney (JIRA) I think I took an idea I had and put it in place of an idea that you had, based on the label and not the description. Sorry about that 🙂. I deleted my comment to clean up the ticket. I will look at PatternSynonyms in ghc also. Thanks! |
Comment by Daniel Sweeney (JIRA) Still thinking about this. Is this where you're going with this?:
Am I getting closer there? I am not sure that the |
That's mostly it. These could also be used for enums/classes/protocols, but I anticipate the primary use to be for structs. I hadn't thought about 7.4, that's a good point. It could be allowed in case the computed property has a For |
Comment by Daniel Sweeney (JIRA) Thinking more, it seems like the implementation for this would be: 1. Make the parser accept cases in struct declarations and extensions to structs. 2. Add the struct cases to the AST for struct declarations. 3. Type check the struct cases in the declarations, inferring, validating, adding the types to the type environment, like for everything else. 4. Change the type checking of a switch statement so that it accepts valid struct cases and rejects invalid struct cases. This might only be type checking, but there might be some parsing/AST changes for switch statements. 5. Glue together the switch cases and the struct cases. On the one hand, you could do a syntax transformation that put the I'm assuming in the above that after type checking that the SIL is generated and that that is where most of the heavy lifting takes place. I'm not sure I'm correct about that, I have not looked that closely at that part of the compiler, but switch statements have so much control flow that you'd (I'd) expect a change to them to be in the SIL section of the compiler. Thinking about it more I'm not sure in implementation terms whether this works like enums at all though, it might have to be almost all new code. SIL has internal support for enum cases with like |
It's probably clear from context, but just pointing out for clarification: this is a wishlist item/feature request, not a 'someone needs to implement this and submit a PR and we can land it'. It would certainly need to go through Swift Evolution. Implementation-wise, what you've described should mostly be fine except I think for an initial implementation, one probably doesn't need special SIL instructions and you can SILGen the matcher as a function; if the inliner deems the matcher implementation to be sufficiently small, it can inline it and do further optimizations. One would also need to implement a mangling scheme. |
Comment by Daniel Sweeney (JIRA) Right, it was pretty clear that it would need to go through evolution. I feel like there is a gap around structs and switch-case matching and was interested in thinking it through. I will keep thinking about it. |
Previous mentions of computed cases on the forums: |
Additional Detail from JIRA
md5: 98206919fda80539e5d50e9c8f42f26c
Issue Description:
Swift's structs are pretty cool. You can project stored and computed properties with
.
syntax, and you can construct values using initializer syntax. Swift's enums are pretty cool too. You can pass in associated values for construction, and you can pattern match and create bindings for the associated values. Enums can be more struct-like where they have computed properties, which are accessed using.
syntax. However, structs cannot be enum-like; they can't have computed cases![]( Oh, the tyranny)More seriously, there is some affordance here in the form of
~=
. However, it's not enough. One wants to work with structs and have the ability to destructure the input in a nice way. Consider the classic example of deserialization, where you put a bunch of different objects into the same array.You have this cascade in a bunch of places. It would be so much nicer if you could write some "computed cases" once, and directly pattern match on
blob
:As an extension, we could have an attribute, say (strawman name)
@uncheckedAssumeExhaustive(case1, ... caseN)
which can be used to say "these patterns are exhaustive, trust me".The text was updated successfully, but these errors were encountered: