-
Notifications
You must be signed in to change notification settings - Fork 205
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
Allow variable binding on String patterns #4158
Comments
Sounds more like a job for a Say:
You can get something like that behavior using extensions. Pattern myPattern = RegExp(r"^/books/(.*)/author$");
extension on String {
Match? get myMatch => myPatten.allMatches(this).firstOrNull;
}
extension on Match {
Match get $0 => this[0]!;
Match? get $1 => this[1];
// ...
}
...
if (myRoute case String(myMatch: Match($1: var i!)) ...
A little cumbersome, but possible. Getting to introduce a new string-pattern formalism like '/books/${final id}/author' It's likely to be insufficiently powerful for a bunch of problems, and still had to answer design questions like how '/books/${final id}/${final name}' should match If we can leverage any |
A simpler approach is to use Uri switch (Uri.tryParse(myRoute)) {
Uri(pathSegments: ['books', final id, 'author']) => 'The author of the book with ID $id',
Uri(pathSegments: ['books', final id]) => 'Book with ID $id',
Uri(path: '/books') => 'Books list',
_ => 'Invalid route',
} |
This is just an example. The pattern would not be something specific for uris. |
I get that. But I think the logic can be used everywhere I wonder if a general solution could be to allowed defining variables inside switch (string) {
case Regex(...).allMatched(string) ; [final Match match]:
print(match);
} |
You can use named groups in your RegExp. Write a helper function that extracts all named groups from the |
I know there are workarounds for this, but I still think that a proper pattern matching against a String (or even better, against a Regex) would be valuable. For instance, @lrhn workaround works [sic], but you have to make an extension for every Regex that you want to use as a pattern, which is not really scalable. If we had Regex literals (which IIRC is a non-goal), this could be way more ergnonomic. |
It would be almost impossible to design this feature correctly. E.g. consider your own motivating example: switch (myRoute) {
'/books/${final id}/author' => 'The author of the book with ID $id',
'/books/${final id}' => 'Book with ID $id',
'/books' => 'Books list',
_ => 'Invalid route',
} Suppose your route is |
^ technically you could do
|
Yes, if the route was As demonstrated by @rrousselGit, it would be possible to do it. And if we had access to Regexp patterns, than it would be even easier. |
In general, you will have to invent "when" conditions of increasng complexity, which, in general, will again be regexps. This is very inefficient and defeats the purpose of the feature. Named groups is more promising idea IMO. For this, you don't need a language feature - a library function will suffice. |
@tatumizer Could you exemplify a library function that would work with as good ergonomics as a language feature? All workarounds provided in this issue, although functional, are considerably cumbersome. The raison d'etre of this issue is not to say that it is impossible to do it today, but to have something better than what we have today. |
I did make I want a few more features for patterns. The primary one being generalized selectors in object patterns: The last then just requires: extension MatchPattern on String {
Match? match(Pattern pattern) => pattern.allMatches(this).firstOrNull;
} and a way to create constant patterns, which you can even do for ... case String(match(const ConstantRegExp(r"....")): Match([1]: var s, [2]: var t)) |
@mateusfccp : when you say "language feature", what feature do you mean? You havent' designed it. Could you show how the program can process this regexp using a hypothetical language feature? |
I didn't find a duplicate for this issue, but GitHub's search is terrible, so this may be a duplicate.
I was thinking that we could allow variable bindings to String patterns. For instance:
We could do it in every way that it's possible today to do with other patters, and if we could even extend it to support assignment.
The text was updated successfully, but these errors were encountered: