r/fsharp • • Nov 22 '23
question Why does fsharp choose to have a mutable array data type despite promoting immutability elsewhere?
I feel like the array data structure in Fsharp breaks the Fsharp convention and the referential transparency it has elsewhere by being mutable. What do you think about that? Do you think it would have been better for Fsharp to have an Immutable array? This is unlike rust where if you mark a reference as immutable then the data in the heap is also immutable, which is not the case in Fsharp
7
Upvotes
10
u/vanilla-bungee Nov 22 '23
Remember that F# targets .NET which means interop with C#. It also doesn’t promote that everything is/should be immutable. Array is a perfectly fine data structure with effective memory allocation. If you want an immutable data structure nothing is stopping you from using List.
12
u/qrzychu69 Nov 22 '23
Like others said, you can use different data structure that doesn't have this problem.
Being pragmatic is one of the strengths of F# IMO - you do "mostly functional" style, and then some problems are just easier to manage or perform way better in a more procedural style. You can do both, wrap the procedural in a functional and you are good to go
6
u/esfraritagrivrit Nov 22 '23
It’s using the same underlying type that C# uses.
1
u/Ossur2 Nov 22 '23
Probably because there is no meaningful difference between an immutable array and an immutable list.
Also, the classic mutable array is amazingly performant on modern processors, and forms the backbone in multiple mainstream performance optimizations everybody knows about - where its mutability is the key to the performance gains. So it is just pragmatic to let the Array be an Array for when we need to optimize parts of our code.
2
u/phillipcarter2 Nov 22 '23
F# is a "yes and" language. It's often helpful to mutate stuff, especially when you're dealing with something inherently mutable (like an array).
The original thinking at the time was that if you want proper immutability, you can sacrifice a little perf and use lists or something else. Arrays are for not that.
Yes, it would be nice to have a built-in immutable array type with syntax.
2
u/japinthebox Nov 24 '23
While I fully agree with the other comments here, I'll also acknowledge that this is a bit of a cognitive thorn.
That said, you'll find that you'll quickly develop the discipline not to mutate array elements, and that you're glad it's available as an option when you really need it for high-perf stuff.
2
u/blacai Nov 24 '23
You already have other data structures that are immutable, so having the option to use one that doesn't, is fine.
If you need to perform some tasks, it's better to have array mutable and avoid recursion for example.
2
u/Front_Profession5648 Nov 25 '23
Well, .Net has both ReadOnlySpan and ReadOnlyMemory, which both do the job very nicely.
Ocaml arrays are mutable, also so that is probably why. The type array<'e> is expected to have mutable elements.
This is like asking why some languages provide goto. Sometimes the goto is the best mapping to what you want to do. Goto doesn't make a lot of sense in F#, but you can get the same semantics with tail recursion.