-
Notifications
You must be signed in to change notification settings - Fork 424
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
Should we deprecate casting strings to regular expressions? #14694
Comments
The big caveat to my response is that I've hardly ever worked with regular expression libraries in languages, only with regular expressions on command-line utilities. For me |
It seems like this issue has been decided and implemented, as compiling the provided example results in:
while using: use Regex;
var s = "contains";
var rs = compile(s);
writeln("contains regular expression".search(rs)); // matches
writeln("doesn't contain regular expression".search(rs)); // doesn't match results in:
Can we mark it resolved and close it? |
Nope. The issue probably predates adding bytes-based regular expressions and making use Regex;
var s = "test";
var r = s:regex(string);
writeln(r); This cast just calls FWIW, I still support deprecating this. @bradcray -- I don't remember whether we've discussed I did a quick look around. Python has the same My argument against using cast for this is still the same: regular expressions are not strings, they are just related. And casting should be a much more simple operation than creating a regex parser. |
We did discuss it and continue to express reservations about the name, but I don't recall what we concluded either, if anything. Checking my notes isn't suggesting any conclusive decisions, though the last thing I typed was a I'm not sure I necessarily buy the argument that casts should only be between related types or involve simple operations. But I'm not insistent that we use a cast either. I just find it a succinct and clear way to say "make me a this from a that." Initializers and copy initializers are obviously similar in that regard (though initializers are not always as 1:1 as casts and copy initializers). |
It is interesting that you bring up initializers. I wonder if there's something we can decide based on the "levels of conversions" idea: #17200 Which suggests that if you can initialize X from Y, you should be able to cast Y to X, if I am understanding correctly. So, that would support your point a little bit in the sense that if I can do |
I agree it feels related. I admittedly haven't internalized the "if you have a, you should also have b" hierarchy of conversions yet, but just wrote some tests that defined an var myX: X = myY;
...myX... rather than simply: ...myY: X... That said, I'd agree that |
Oh one other thought here. In conversations sometimes, I feel as though people (not saying you, necessarily) often interpret a cast as being "pretend that this Y is an X without actually creating a new X", which I don't think is accurate in general, particularly for value types. I think classes in a subclass/superclass relationship are one of the only places (in Chapel at least) where casts avoid creating new values (?). This is part of why I tend to view casts and copy initializers as being quite similar apart from one (the cast) being more explicit than the other. |
I think I am one of those people, though, and it came up before. I am still trying to rewire my brain. Not to argue against what you said, but more as an introspection: my mental reflex is still that casting is almost like the programmer telling the compiler "forget what you know about this value, pretend it is of type X", which is quite different than what you suggest and I believe it is the reason my response is different than yours. |
Today, At the same time, #17200 indeed only considers A more relevant example for #17200 is that if we want var x: regex = "myString"; to compile, then we need to also allow var x = "myString" : regex; I don't personally think we need to support either of those though (and I lean more towards just having people call |
I thought the general sense of things at the regex review was that people considered |
Discussing this question in a sub-group during module review, we noted some of the motivation for this question. Motivations:
This also relates to the question of dropping Because throwing initializers remain a Polls taken during the re-review indicate preference for dropping the cast from Questions/Polls:
The presumed result of the discussion is that we will deprecate the cast from |
Recall that while we don't support throwing initializers today, we have written demonstrations that have shown that we're very close to supporting throwing initializers as long as the throw is done after the logical [edit: So in saying that, I'm suggesting maybe we should grit our teeth and get the remaining 10-20% done, under those constraints, if it's something we want for cases like this] |
#21636) This PR adds a new throwing initializer for `regex` and deprecates `Regex.compile`. Resolves #17187 Also see #14694 (comment) While working on this, I bumped into an issue while replacing a `compile` call in ChapelBase. That part of the code already had a TODO. So, in the end, I moved `_command_line_cast` inlining its helper from ChapelBase to ChapelUtil and addressed the problem referred to in the said TODO. This enables the helper to `use Regex` without causing resolution order related problems. This also relates to #5456. [Reviewed by @vasslitvinov] Test: - [x] standard - [x] gasnet
Currently, one can create a regular expression by casting a string:
On #14686, @mppf suggested that may be we should deprecate these and make
compile
the only way to create a regular expression (these casts callcompile
with default arguments).Arguably, regular expressions more than just strings and being able to cast a string to regular expression feels weird to me, too. I don't think it brings any convenience either.
The other way around (casting a regular expression to string) can be seen as a handy way of getting the actual expression from a regexp object, so it doesn't feel that weird. However, if symmetry is an issue, a user-facing way to get the expression from a regexp object can replace that cast, too.
The text was updated successfully, but these errors were encountered: