-
Notifications
You must be signed in to change notification settings - Fork 535
Replace regex with pattern as string to support OTP 28 #1360
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
Conversation
Hey @TBK145 is there some Elixir issue tracking this? Recompiling the regex on every function call is going to be much worse for performance. |
@valid_name_regex "^[_A-Za-z][_0-9A-Za-z]*$" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@valid_name_regex "^[_A-Za-z][_0-9A-Za-z]*$" |
@valid_name_regex | ||
|> Regex.compile!() | ||
|> Regex.match?(name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@valid_name_regex | |
|> Regex.compile!() | |
|> Regex.match?(name) | |
Regex.match?(~r/^[_A-Za-z][_0-9A-Za-z]*$/, name) |
This is a change on Erlang/OTP 28, we can no longer precompile them in the module body, but we only need to move it inside. Elixir v1.19 will emit a warning telling you to do exactly that. |
Hi @josevalim , at first I wanted to solve it in the way you propose, but the regex is also referenced here, so I did it in this way to only have to maintain the regex in one place. |
In this case it is best to move the regex to a private function and reuse it. :) |
That was actually the first thing I tried, but since it's a module attribute, the function is not defined yet before calling. |
What I mean is this:
And use it as |
I understand, but what I mean is that the regex is also used in module attribute |
You can also move it to a function. I assume there isn't much benefit for those being attributes anyway, besides user convenience. |
OTP 28 changes the implementation of the regex module, and as a result you can no longer use regexes as module attributes.
Thank you @josevalim for the assist! |
OTP 28 changes the implementation of the regex module, and as a result you can no longer use regexes as module attributes.
By changing the regex to a pattern as a string it can still be used in the error message, and just needs to be compiled before usage.