Skip to content
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

Reuse Replacement in Regex.replace #164

Open
i-am-the-slime opened this issue Nov 7, 2022 · 2 comments
Open

Reuse Replacement in Regex.replace #164

i-am-the-slime opened this issue Nov 7, 2022 · 2 comments

Comments

@i-am-the-slime
Copy link

Right now the signature of replace on regex is:

foreign import replace :: Regex -> String -> String -> String

and I argue it should be

foreign import replace :: Regex -> Replacement -> String -> String

Of course this would be breaking so potentially there should be an alternative under a different name and the old one could be made deprecated.

What do you think?

@MonoidMusician
Copy link
Contributor

I actually wouldn't use the Replacement type here, because it is not a literal string being inserted but a string with special syntax:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_string_as_the_replacement

So I guess if you want to be specific, it should take a new type, and we should expose functions for building that type according to the syntax or something. I personally don't care enough to do that 😅

@michaelficarra
Copy link
Contributor

it should take a new type, and we should expose functions for building that type according to the syntax or something

This would be nice, actually. Not only would it make it easier to use the magic patterns by name instead of having to look them up every time, but it would also allow consumers to not worry about having to escape $ if they're trying to include a literal $ in their replacement. I think it's worth it.

If wanted to give consumers an unsafe out to avoid having to build up a sequence of these "replacement parts", you could have a function that parses a replacement string into the sequence for them automagically. Seems like you get the best of both worlds that way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants