Remix.run Logo
lupire 8 days ago

This is the same problem people have with closures, where it's unclear to the user whether the argument is captured by name or by value.

layer8 8 days ago | parent | next [-]

This isn't the same problem, because this is about whether the regex is instantiated each time the code around the regex is executed, or only the first time and cached for subsequent executions. The same could in theory happen with closures, but I haven't ever seen programming-language semantics where, for example, a function containing the definition of a closure that depends on an argument of that outer function, would use the argument value of the first invocation of the function for all subsequent invocations of the function.

For example, when you have

    fn f x = (y -> x + y)
then a sequence of invocations of f

    f 1 3
    f 2 6
will yield 4 and 8 respectively, but never will the second invocation yield 7 due to reusing the value of x from the first invocation. However, that is precisely what happens in the article's regex example, because the equivalent is for the closure value (y -> x + y) to be cached between invocations, so that the x retains the value of the first invocation of f — regardless of whether x is a reference by name or by value.
ethan_smith 7 days ago | parent | prev [-]

The parallel is apt, but regex /o is more like a closure that captures by value at declaration time rather than an ambiguity between capture strategies.