Why there is no F# immutable arrays?
Asked Answered
S

2

6

I am new to F# and I noticed that arrays are still mutable. Is this because of the performance implications or because the inherent .NET CLR type system? I am aware that immutable lists do exist.

Thank you

Sectionalize answered 29/8, 2021 at 11:40 Comment(0)
W
4

Many people agree with you that F# should have immutable arrays, including the language creators; there's an issue covering it in the fslang-suggestions clearing house, currently in approved-in-principle state.

As for why it wasn't there from the start, it was likely just a low priority compared to other features.

Whooper answered 24/9, 2021 at 17:13 Comment(1)
Updated RFC link: github.com/fsharp/fslang-design/blob/main/RFCs/…Sievers
G
1

Like OCaml, F# is a pragmatic instead of a pure language. This is what some people, like me, like the most on these languages.

In OCaml, arrays are mutable, and F# bridges the OCaml and the .NET CLR libaries. It makes perfect sense, to have mutable arrays then.

Why does ocaml have mutable arrays?

For beginners, this bridging between the 2 worlds is certainly a bit disturbing, the best is to take things as they are and carry on with learning the language. As said at the beginning, think pragmatic not pure.

I am not sure about your saying about mutable lists. F# lists are immutable. You can use .Net CLR System.Collection.Generic.List which is mutable. But when talking about lists, FSharpers mean the immutable [], [1..3], or by name: Microsoft.FSharp.Collections.FSharpList`1 .

F# collections:

https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/fsharp-collection-types

Immutable core types are list, map and set.

Grouse answered 29/8, 2021 at 12:39 Comment(1)
Immutable arrays is in rfc stage. github.com/fsharp/fslang-design/blob/main/RFCs/FS-1094-block.mdNaught

© 2022 - 2024 — McMap. All rights reserved.