tc39 / proposal-regexp-named-groups

Named capture groups for JavaScript RegExps
https://tc39.github.io/proposal-regexp-named-groups/
222 stars 21 forks source link

Argument order for callable replaceValue #16

Open schuay opened 7 years ago

schuay commented 7 years ago

As a suggestion to improve ergonomics of @@replace calls with a callable replaceValue: passing namedCaptures to replaceValue as the second argument would allow a generic

function replacer(match, groups) {
  // Use captures from the groups object.
}

to handle RegExp's with varying capture counts.

This would be an improvement over the current state where one needs to either adapt the replacer signature for different capture counts, or use the arguments object.

hashseed commented 7 years ago

Rest parameters would be an alternative with the current proposal, but it doesn't seem concise nor ergonomic.

In hindsight, having variable length arguments in the middle of the arguments list was a bad idea in the first place.

littledan commented 7 years ago

@schuay Slight tweak: Do you still want the position and entire string as arguments? They could go after the groups object, maybe.

We'd previously considered various other argument positions. This proposal is based on the idea that compatibility isn't important since we're only changing the argument order of RegExps with groups in them. We got to Stage 3 on the conservative argument ordering, but I'm wondering if people might still consider this one. cc people active in past conversation about this: @ljharb @bterlson @allenwb @michaelficarra @BrendanEich

schuay commented 7 years ago

@littledan I wasn't suggesting changing other replaceValue arguments. I guess it may be valid to do so, but I'm not aware of the history on this; and it doesn't sound right to me to completely change the replacer signature depending on whether named captures exist or not.

ljharb commented 7 years ago

I think compatibility is very important, if possible to obtain, because old replacers will start getting called with new regexes.

littledan commented 7 years ago

@ljharb Do you have an example of this? It's hard to imagine how they will be used, as different RegExps have different numbers of numbered groups.

ljharb commented 7 years ago

@littledan (sorry for the delayed response) a replacer that reflected on the arguments object: Array.prototype.slice.call(arguments, -2) would always return [position, string], and Array.prototype.slice.call(arguments, 0, 1) always the matched item, and Array.prototype.slice.call(arguments, 1, -2) always the list of captures.

hax commented 7 years ago

@littledan There will always be edge cases break compatibility whichever position we choose.

If JS support (...args, last) like Python, there will be no ergonomics problem. Is that path possible?

littledan commented 7 years ago

@hax As far as ergonomics goes, arguably it's prettier to not have the ...args first, isn't it?

hax commented 7 years ago

@littledan Sorry, I should @ljharb in last comment.

About ergonomics, I'm just curious why JS does not support (...args, x, y), I have several times see some programmers write similar code and they were surprised to got syntax errors.

littledan commented 7 years ago

@hax Yeah, that seems like a good idea, and I don't see why we couldn't do it. Maybe we could get into details on another thread though.