-
Notifications
You must be signed in to change notification settings - Fork 9
Concerns regarding type inference and data-last functions #18
Comments
Thanks for the well-written issue! Your points are good and well taken. There’s two things to address here. TypeScript unification’s limitationsOne is a general problem with TypeScript not unifying types in It’s interesting how I’m hopeful that TypeScript will improve its type unification so that it retains free types (microsoft/TypeScript#30134), but of course this means that we can’t do (CC @tabatkins: This is may be a problem with our “we can just tack on Whether to reintroduce split mix (
|
I don't understand why
Yes, adding it to this version of the proposal would almost certainly sink things. I'm happy to explore more syntax after the dust has settled on the base proposal and we see how it works in practice and how the ecosystem adapts to it. |
Yes, the vast majority of JS is not written in curried, data-last style; that's limited to a relatively small subset of users of certain libraries (popular libraries, to be sure, but nowhere near universal). Most JS (importantly, any JS written using web platform APIs) is written with non-curried data-first (or at least data-early; argument order isn't always well thought-out...) functions, and the nesting problem is just as prevalent there. (In particular, variadic functions, and final optional argument-bag functions, are very common on the web platform and in JS as a whole, and they're wholly unsuitable for auto-currying; that style requires a certain discipline and design style that JS isn't particularly built to encourage or enforce.) Making sure that the pipe operator addresses the large majority of code as well as it possibly can is more important than optimizing it for the curried-data-last paradigm (at least in the first iteration of the syntax), particularly since that paradigm already has a working and syntax-light library solution in the form of |
In anticipation of the archival of this repository and switching back to https://github.com/tc39/proposal-pipeline-operator, I’ll close this issue. Thanks again for raising it; hopefully microsoft/TypeScript#30134 gets resolved soon. |
Unlike the F#-style, the Hack-style is incompatible with the majority of pipeable APIs that already exist—data-last functions in widely used libraries such as RxJS and fp-ts.
In TypeScript, type inference works left to right. In this example the type of the
user
argument can't be inferred because the data comes afterwards (i.e.map
andfilter
are data-last functions).This is not a problem with the
pipe
function (currently used in RxJS/fp-ts) because of contextual type inference, so this compiles:To make type inference work, pipeable APIs will probably need to be rewritten so they are data-first instead of data-last:
However this makes partial application more awkward. With data-last functions we can partially apply them so the data is provided later on:
Partial application with data-first is much more verbose because we need to include a function signature:
Given that the data-last approach makes sense regardless of pipelines (e.g. for partial application), and given that the data-last approach has been widely adopted in popular libraries such as RxJS and fp-ts, I think it's important that we consider how this pipeline proposal can provide interoperability. After all, these FP libraries represent the majority of the
pipe
function usage that the pipeline operator is supposed to replace.The
|>>
operator ("tacit unary function application") does not suffer from these problems—it works with data-last functions. For this reason I would like to suggest that we bring the|>>
operator forward so it is part of the initial specification. Example usage with data-last functions:My personal preference would be for the F#-style operator but given the Hack-style operator seems to be progressing I hope we can bring the
|>>
operator into the specification sooner rather than later. Failing this I worry that many libraries will continue to use thepipe
function.The text was updated successfully, but these errors were encountered: