Skip to content

Instantly share code, notes, and snippets.

@asolove
Last active July 29, 2016 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asolove/4d9ecd87dacc6c235a1e43ae33e8ad57 to your computer and use it in GitHub Desktop.
Save asolove/4d9ecd87dacc6c235a1e43ae33e8ad57 to your computer and use it in GitHub Desktop.
Trouble exporting generic curried functions
// I have a library of helper functions that I want to export curried versions of
// A minimal chunk of it looks like this:
// stole this type signature from a PR discussion: thanks!
export function curry2<A,B,C>(f: (x: A, y: B) => C): (x: A) => (y: B) => C {
return (a) => (b) => f(a, b)
}
function _push<A>(item: A, items: Array<A>): Array<A> {
return items.concat(item)
}
export const push = curry2(push)
// But that doesn't work. Flow complains expression `curry2(push)`, saying:
//
// - type parameter 'A' of function call. Missing annotation.
// - type parameter 'B' of function call. Missing annotation.
// Possible solution #1: annotate the type of `push`
export const push<A>: (item: A) => (items: Array<A>) => Array<A>
// But this doesn't work because const expressions can't introduce generic type variables.
// Possible solution #2: export a wrapper function?
export function push<A> (item: A): (items: Array<A>) => Array<A> {
return curry2(_push)(item);
}
// But at that point I'm basically re-writing most of `curry` myself for every function I export.
// Is there a better way?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment