-
-
Notifications
You must be signed in to change notification settings - Fork 503
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Array.last
: add support for ReadonlyArray
#544
Conversation
<A>(as: ReadonlyArray<A>): Option<A> Now the question is: should we support @sledorze @raveclassic what do you think? What's your experience with the p.s. |
For context, I'm receiving a |
Indeed, theoretically using p.s. |
I never use ReadOnlyArray but when a lib exposes it. (we also never mutate but in lowlevel functions with care of keeping referential transparency) The general reason tu support ReadOnlyArray is to integrate with third part lib. Note: I'm interested into knowing more about the refactor of errors goal in |
Proposal 1: if +export const last = <A>(as: ReadonlyArray<A>): Option<A> => {
-export const last = <A>(as: Array<A>): Option<A> => { otherwise add an overloading +export const copy = <A>(as: Array<A>): Array<A>
+export const copy = <A>(as: ReadonlyArray<A>): ReadonlyArray<A>
+export const copy = <A>(as: ReadonlyArray<A>): ReadonlyArray<A> => {
-export const copy = <A>(as: Array<A>): Array<A> => { However I'm not sure how to deal with something like: export const getMonoid = <A = never>(): Monoid<Array<A>> => {
...
} or something like export const range = (start: number, end: number): Array<number> => {
...
} Proposal 2: Add a /cc @RoryDungan |
@gcanti mentioned previously that, while Array contains all properties of ReadonlyArray so changing function arguments to take ReadonlyArrays wouldn't break backwards compatibility, it would cause an issue with if we changed existing functions to return ReadonlyArrays. The solution he proposed was adding overloaded versions of each array function that handle ReadonlyArrays, but another option I think could be worth considering would be to change existing functions that take Arrays as arguments to take ReadonlyArrays, but leave them to return regular Arrays. As far as I can tell all fp-ts array functions return a new array separate from the source one so having them be different types shouldn't cause an issue. Since Array is a superset of ReadonlyArray, functions of type |
93de5eb
to
6f0676d
Compare
I'm working on an application that uses https://github.com/jonaskello/tslint-immutable to keep the other devs on the team honest. One of the rules (which I want enforced) is use of I frequently find myself having to cast them to |
This has also been an issue for me. |
@gabejohnson curious about |
@gcanti |
Closing due to inactivity |
Currently this function will error when trying to pass in an array of type
ReadonlyArray<T>
.I suspect we'll want to make the same change for other functions which receive arrays… but it does seem like a PITA.