Converting mutable array into immutable array

How can I take a mutable array, for example the array returned by Array.init, and convert it into an immutable array?

let a: [Text] = Array.init{3, "Hello");

note that the open curly-brace is supposed to be an open parenthesis but this forum won’t let me post it as such; see side note below

The above code {using a parenthesis not a curly brace) gives me the following error {with open parentheses replaced with open curly braces because of forum shenanigans)

type error, 
cannot implicitly instantiate function of type
  <A>{Nat, A) -> [var A]
to argument of type
  {Nat, Text)
to produce result of type
because no instantiation of A makes
  {Nat, Text)  <:  {Nat, A)
  [var A]  <:  [Text]

Side note, this forum is making it very hard for me to post errors. I get a 403 error if I try to post back-ticked open parentheses if somewhere previous is a triple-backticked comma and the word function on the same line within backticks. After type error, in my error post above, I had to either remove the comma, add a newline, or change the word function to something else, otherwise I get a 403 error when trying to post or save an edit. I also can’t post function-like code, including anything that uses an open parenthesis, so I had to change them to open curly braces above. This seems to only happen if a previous back-ticked block contains an open-close parentheses pair. Something must be wrong for me right now because I’ve posted code here before without running into this issue. Anyone have an idea of what might be going on here?

1 Like

I believe you want to use the freeze function.


Ah, perfect. Didn’t notice that function when scanning the docs.

Also, in regards to my “side note”, it seems the issue only occurs when editing a post. When first posting, the strange restrictions don’t apply and you can post whatever you want. Get it right on your first try, I guess, or you’ll need to mess around with parentheses like I had to.

1 Like