aantron / promise

Light and type-safe binding to JS promises
MIT License
341 stars 24 forks source link

Pipe last implementation #53

Closed lessp closed 4 years ago

lessp commented 4 years ago

Previously Promise was pipe-last. Within the BuckleScript the main API's are pipe-first (a separate discussion 😄). For me, who's also interested in the native side, where pipe-last undoubtedly is the norm, what would be your view on providing both options?

aantron commented 4 years ago

It's feasible to do, but I'd be concerned about increasing bundle size on the JS side, for a feature that isn't a "primary" concern for JS-only work. For now, I added a PipeFirst module (you may have already seen it). To write code that works on both native and JS with it, you can do:

https://github.com/aantron/promise/blob/6b9ff51165dbb6c21bc49d54c04d6fa383038717/test/native/test_ffi.re#L8

On JS, the module is empty, because we want to forward to BuckleScript's operator:

https://github.com/aantron/promise/blob/6b9ff51165dbb6c21bc49d54c04d6fa383038717/src/js/promise.rei#L230-L231

On native, the module defines the operator for unary functions (it's the desugared syntax, but -> maps to it):

https://github.com/aantron/promise/blob/6b9ff51165dbb6c21bc49d54c04d6fa383038717/src/native/promise.rei#L222-L224

This is good enough for most uses, which are partially-applied binary functions from Promise.

Obviously, using -> a lot on mostly-native code is going to stick out. I don't know yet what to do about that :/ I figured this library's main contribution is to JS, rather than to native, so it's best to follow BuckleScript conventions.

Note that the pipe operators might become less important over time, as people switch more and more conclusively to OCaml 4.08+ on the native side, and BuckleScript releases a Reason with let* support. Then, let* syntax might replace many uses of the pipe operator with promise functions (https://github.com/aantron/promise/issues/52).

lessp commented 4 years ago

Thanks for always providing such elaborate and thoughtful answers!

I had entirely forgotten about let-operators pretty much solving this completely! Personally, I can see how it makes sense to wait, that considered. 🙂