-
Notifications
You must be signed in to change notification settings - Fork 457
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Uni pipe chaining for imperative APIs #2748
Comments
note mySet |. Set.[|
add 1 ;
add 2 ;
remove 1
|] |
What's the ambiguity of |
the |
cc @jaredly this was the feature I was talking about |
I really like this! Admittedly I think people should just you immutable data structures but when that's not an option then this is sweet. |
Yeah, the goal is not to make the performant paradigm unnecessarily harder. |
so fancy |
Good point @chenglou. I thought about this a little more and it would be easy enough to just define this in plain code: let (|..) = (obj: 'a, lst: list('a => unit)) : unit =>
Belt.List.forEach(lst, fn => fn(obj));
let a = Stack.create();
a
|.. Stack.[
push(1),
push(2),
s => pop(s) |> ignore,
push(3),
iter(Js.log),
]; Pros:
Cons:
Another option might be to just define a function on Belt rather than an operator. Not sure where it would live. |
@justgage this undoes the performance benefits. See the generated code. This pipe operator is specifically syntax and not just a function. Additionally, function doesn't compose with other non-function things. A syntax-level pipe does: |
Ah very good point @chenglou. I forgot about that benefit! |
Correct me if I'm wrong, but given that this is a thing.
Maybe that could be good enough? |
Yeah, though that leaves a tuple in the output |
Yeah, it does, that's why I ignored it. I guess it introduces some room for mistakes. Maybe it would be possible to do so that if you use fast pipe with a tuple of functions and they all return units you don't have to ignore them? I think it would be a win in terms of consistency and less syntax to learn. |
I was thinking just doing like this https://reasonml.github.io/en/try.html?rrjsx=true&reason=PYBwpgdgBAQmA2AXA3AKFfMioFsCeAyllALxQCyArogIYBGmRiAdAJIQs40DWYAFAEo0qfE1QAfZlD6ooFavUZY2HZjQAm6vgEYBAGlnzaDMExUsNWgEz7DVY0pbsWAJzA5gAN366DQ1EA Thought that would give a warning, but it doesn't seem to do so. |
this could be optimized, but when I read tuple, in general, I don't assume evaluation order, it is not the case for semicolons though |
Well the semicolon argument applies only to ocaml so I'm not sure if we want to get too attached to that. And in general I think the expectation is that code executes from left to right and top to bottom (at least for me) so I think it should be ok. Or am I incorrect in assuming such evaluation order in ocaml / reason? |
I'm confused as to why this is necessary — isn't this what the chained imperative statements already do? i.e. the semicolon in OCaml-syntax? |
This is obviously not ready to be merged yet. But my hope is that, once reasonml/reason#2487 is merged, we can get default support for monadic sugar with JS promises in Reason. Expected usage would be like this: ```reason open Js.Promise; { let.async name = resolve("amazeface"); Js.log2("The user's name is " ++ name); } ``` Except, I just realized that I don't think this will work, because the source file is in OCaml syntax, and (let.async) isn't valid OCaml syntax. So this PR may not even be possible. Would you consider the possibility of changing the js_promise bindings source file to be a Reason file after rescript-lang#2748 is merged? I can see that you might be trying to keep Reason out of the source so that pure OCaml users of Bucklescript don't have to mess with Reason. But according to my understanding bsb should be able to compile both Reason and OCaml files in the same project without an issue, right?
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
This feature was held back due to being overly magical and due to
|.
still being new. Upvote this issue if you'd like to see it re-released.The text was updated successfully, but these errors were encountered: