-
Notifications
You must be signed in to change notification settings - Fork 213
Should list patterns explicitly check the length or not? #2415
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
Comments
Reading I like the explicit over the implicit, so I'd go with (I'd actually also want |
Reading |
I agree on the map. It's a structure pattern, so I expect the structure of the pattern to match against the structure of the value. I'd want The type+extractor pattern is different, mainly because it doesn't give away the structure of the object, just its type. (Well, it looks like a constructor, if you squint, but it might not be a constructor which actually exists. Constructors are not isomorphic with object structure.) Maybe we should change extractor patterns to use curly braces,
|
Not sure if my vote counts, but I prefer strict matching. I wouldn't expect |
I've considered that a number of times, but I've never convinced myself that I think the unfamiliar syntax is worth it.
True, but these are vanishingly rare in cases today which suggests that we shouldn't give that nice syntax to the thing almost no one uses.
I do like that it looks like a constructor. In particular, I think it's very important to have some syntax that looks like a classic algebraic datatype pattern as you would see in OCaml, Haskell, Scala, Rust, etc. We could come up with a different kind of "destructor" method that classes can define (like Giving this nice syntax to getter-based extractors lets every Dart class in the wild use it (with the caveat that the arguments have to be named). We can still add support for user-defined destructuring later (#2104) and have it reuse the same syntax by having a user-defined destructurer take precedence over the default behavior.
Yeah, I think if you don't have an identifier before
Your opinion definitely matters! That's a big part of why we do this design in the public repo and issue tracker. :) |
Seems that C# 11 will introduce list patterns too. [var x, ..] // length one or more, capture first
[var x, .., var z] // length two or more, capture first and last
[var x, .. var y, var z] // length two or more, capture first, last, and sublist in the middle. At most one The C# list pattern works on, I think, anything with [var x, .. var y, var z] --becomes-- {Length: >= 2, [0]: var x, [^1]: var z, Slice(1, -1): var y} (or what the syntax would be for doing inspection on the object). |
Forking a new issue from this comment by @eernstg:
The obvious choices are the following:
The two approaches are obviously equivalent, so we should ask people what they think is more useful and readable.
The proposal takes the strict matching approach, and that's the one I currently favor, but I could be persuaded otherwise.
The text was updated successfully, but these errors were encountered: