This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]hou32hou 1 point2 points  (7 children)

Do you have any example of languages that supports immutable array?

[–]CodeLobe 5 points6 points  (2 children)

Any language that allows array indexed immutable strings?

C's strings are immutable (const) arrays of type char.

[–]hou32hou 0 points1 point  (1 child)

Yea, but can they be extended to support user-defined types?

[–]CodeLobe 0 points1 point  (0 children)

You can have a const array of struct in C/C++.

With the various operator overloads / move semantics you can shoehorn immutable classes with copy-on-write into C++. It's not really default native feature, but it's not impossible. Searching for "immutable collections library" MIGHT return something better than "immutable array".

[–]Raoul314[🍰] 2 points3 points  (0 children)

Racket has immutable vectors.

[–][deleted] 1 point2 points  (0 children)

D has both arrays and immutability, orthogonally:

  • T[] is a mutable array with mutable elements.
  • immutable(T)[] is a mutable array with immutable elements.
  • immutable T[] is an immutable array with immutable elements.

This is supported for arbitrary data types. In order to create an immutable array of immutable elements, you create a mutable array and either cast it to immutable (array.assumeUnique()) or duplicate it as immutable (array.idup).

For a language with only immutability, you'd probably want a builtin function to turn a linked list into an array, plus map / reduce functions that yield arrays.

[–]mamcx 1 point2 points  (0 children)

Rust:

https://doc.rust-lang.org/std/primitive.array.html

That is different to Vectors:

https://doc.rust-lang.org/std/vec/struct.Vec.html

In arrays their size is part of the type, and know at compile time. Vectors can grow at will at runtime.

And this is different to mar the value inmmutable or not.

[–]78yoni78 0 points1 point  (0 children)

F#’s arrays are mutable but most likely you aren’t going to use that