Hi, I am new to rescript. I have encounter a problem when trying to use Js.Re.captures with Option.map. I have attach an example below. The compiler complains that Js.Re.captures is a curried function, and it compiles after simply warping the original function. I believe there is nothing wrong with Js.Re.captures.
let filename2length = name => {
let match = %re("/(\d+)([A-Za-z]+)/")->Js.Re.exec_(name)
Option.map(match, Js.Re.captures)->ignore
None
}
In your 2nd function, you passed Option.map a function with no argument–that is why you are getting the complier error: “This function is a curried function where an uncurried function is expected”. Option.map needs a function that takes an argument, which is why your 1st function worked. When I do this for your 2nd function:
let filename2length2 = name => {
let match = %re("/(\d+)([A-Za-z]+)/")->Js.Re.exec_(name)
Option.map(match, Js.Re.captures(_))->ignore
None
}
It gives Javascript that will produce the same result as your first function.
Thank you for your explanation. But I am still not clear why a “_” make a difference, isn’t Js.Re.captures receive the only one argument, as documented in its signature?
The underscore here is a placeholder that tells ReScript I need an argument but I can’t give you a variable or the compiler will yell at me.
For a more professional explanation you can read about it here: https://rescript-lang.org/docs/manual/latest/pipe#pipe-placeholders
Basically if Option.map needs a function f(x) for the 2nd argument, you effectively gave it “f” without the “(x)”, so it complained about receiving a curried function. But because you can’t give ReScript “x” here because it doesn’t exist, so you have to 'fake it out" with the underscore. If you look at the Javascript it produces you see that it actually replaces the Js.Re.captures with an anonymous function.