rescript-compiler icon indicating copy to clipboard operation
rescript-compiler copied to clipboard

Piping into variant constructors only works for a single argument

Open ccapndave opened this issue 4 years ago • 4 comments
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.

ccapndave avatar Mar 18 '21 17:03 ccapndave

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"))
}

ccapndave avatar Mar 18 '21 17:03 ccapndave

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

flash-gordon avatar Mar 25 '21 11:03 flash-gordon

Perhaps related: piping does not seem to work with uncurried, currently.

cristianoc avatar Jun 25 '22 23:06 cristianoc