rescript-compiler
rescript-compiler copied to clipboard
Piping into variant constructors only works for a single argument
trafficstars
For example:
type actions =
| OneThing(string)
| TwoThings(string, int)
let thisWorks = AddThings("dogs", 5)
let thisWorksToo = "dogs"->OneThing
let thisDoesn't = "dogs"->TwoThings(5)
I know that constructors aren't really functions and get desugared into tagged objects, but if we are going to pretend they are functions like in ML languages then it would be nice to go the whole way and allow piping into the first argument.
An example of a specific case where this would be lovely is https://github.com/bloodyowl/rescript-react-update where it would become possible to write:
switch action {
| Increment => {...state, counter: state.count + 1}->Update
| IncrementAndLog => {...state, counter: state.counter + 1}->UpdateWithSideEffects(_ => Js.log("blah"))
}
There's underscore for placing function arguments when piping. This doesn't work for variant constructors though
type foo = | First(int, string);
let first = (a, b) => First(a, b);
Js.log("bar"->first(5, _)); // works
Js.log("bar"->First(5, _)); // doesn't work
Perhaps related: piping does not seem to work with uncurried, currently.