Skip to content

Conversation

@sabiwara
Copy link
Contributor

Complements #14907

Comment on lines +289 to +290
Regex.import(~r/foo/E)
~r/foo/
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Real doc test doesn't work because of ref comparison


{:re_pattern, _, _, _, _} ->
if Code.ensure_loaded?(:re) and function_exported?(:re, :import, 1) do
raise ArgumentError, "Expected an exported Regex, got: #{inspect(regex)}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think already imported regexes should be a no-op. This will allow consumers to write code that always get an imported regex at the end, without having to worry if the caller gave an exported regex or not.

Copy link
Contributor Author

@sabiwara sabiwara Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, this was what I originally had in mind in our previous discussion.
I agree this would be more convenient and there shouldn't be a real downside.
My only point of concern is that the import name doesn't hint at this behavior, but I don't have a better idea in mind (ensure_imported? meh) and it's probably fine.

@josevalim
Copy link
Member

josevalim commented Nov 14, 2025

@sabiwara looks good to me!

In theory, because our regexes have the source and compile opts, they could be recompiled too. This would be useful in cases where you wrote the full regex (without export) somewhere, and now you need to make it work again.

Therefore, we could have this:

  • recompile(exported) -> becomes imported
  • recompile(already_compiled) -> recompiles

On the other hand, Regex.import has a clear benefit that it can be a no-op if it is already imported. But it doesn't deal with the case where old regexes stored somewhere...

With all that said, I think the current action plan is good:

  • Add import
  • Deprecate recompile. If someone needs this functionality, they can still do Regex.compile!(Regex.source(re), Regex.opts(re))

Anyway, I thought I would write my thoughts down for future discussions.

@sabiwara
Copy link
Contributor Author

Done in 5efccba.

I wonder if we should add the symmetrical for completion, although I'm not sure what the use-case would be. Sending over a node a dynamic regex?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants