Readonly arrays are great to prevent accidental mutations via sort method, but PITA when used with code requiring normal Array. Long story short, it's: Use it everywhere, nowhere, or cast all the time.
I didn't find any other frictionless solution from the DX perspective than not using readonly arrays at all.
I believe it should be mentioned in the documentation.
It's ok to accept ReadonlyArray or even ArrayLike as an argument but things get worse when using them as a return type.
I think the best solution so far is to accept ArrayLike and return Array if a function constructs a new Array.
Note: we have the same problem in most https://github.com/mostjs/core/pull/296#discussion_r326129838
@gcanti ReadonlyArray really should be banned in fp-ts ecosystem. It's only pain without any measurable benefit. I just spend another hour with export interface Context extends ReadonlyArray<ContextEntry> { trying to figure out why I can not use fp-ts Array. Not I have to cast (ugly) or call slice(0) (ugly too).
ReadonlyArray really should be banned in fp-ts ecosystem
In practice it already is (except for io-ts's Context, for legacy reasons)
Sorry, but I don't get why the use of ReadonlyArray (a built-in type) should be discouraged in the fp-ts docs.
Besides, I'm not sure I understand the reason to artificially limit the ReadonlyArray usage, given that the functions in Array.ts are pure.
ReadonlyArrays are great, and I'd be glad to submit a PR to support them more (either with overloads to return ReadonlyArrays when operating on ReadonlyArrays or using ArrayLike and returning Arrays as @raveclassic suggested) if that's ok with the maintainers.
"ReadonlyArrays are great" if everything is read-only array otherwise poor developers have to cast them all the time. If fp-ts people are happy with it casting all the time or not using mutable arrays at all, then ok.
In our projects we're enforcing using readonly and ReadonlyArray with functional linting rules.
Actually I'm considering to drop support for mutable modules in v3, https://github.com/gcanti/fp-ts/issues/1226
Most helpful comment
Sorry, but I don't get why the use of
ReadonlyArray(a built-in type) should be discouraged in thefp-tsdocs.Besides, I'm not sure I understand the reason to artificially limit the ReadonlyArray usage, given that the functions in Array.ts are pure.
ReadonlyArrays are great, and I'd be glad to submit a PR to support them more (either with overloads to return ReadonlyArrays when operating on ReadonlyArrays or using ArrayLike and returning Arrays as @raveclassic suggested) if that's ok with the maintainers.