> I know that an openArray in proc parameters is read only, but the nim
> compiler will copy its values to result if compatible.
No it will not copy anything. Nim Arrays work like C arrays. It would be
equivalent to the C code:
void *arr= const void* openArray
Run
It creates a mutable reference, which cannot happen. It's similar to a Rust
borrow.
> You can do that because I've used an array instead of a seq[Val] in my small
> example. When using a dynamic array whose size is not known at compile time,
> you can't write such code (and that's the reason I used an openArray in my
> example).
You can write such code! It's easier with a seq[Val] because it computes the
size at runtime.
Here is your first example using seq:
[https://play.nim-lang.org/#ix=23Qp](https://play.nim-lang.org/#ix=23Qp)
Here is your second example with seq:
[https://play.nim-lang.org/#ix=23Qr](https://play.nim-lang.org/#ix=23Qr)
> I should have been able to call the initGa proc with an array[int] and return
> an array[Val] as int and Val are compatible.
No! That should not be allowed at all. Int and Val are not compatible! They are
distinct types! This is an important feature of Nim that prevents mistakes.
Specifically, in your example the Int(0) is not a valid Val type. Nim prevents
you from making this mistake.
Nim types are like a Ada types. Just because two types are binary compatible
does not mean they are semanticaly compatible (this is why we have the distinct
keyword like type meter = distinct Int You cannot convert an int type to a
meter type without explicit casting. Range type works the same way.
> As Board items default values '0' are not in Val, the compiler will complain
> with the [ProveInit] warning, and because that's a multidimensional array, I
> can't tell the compiler that it is initialized if I use multiple
> initialization loops.
You would not use multiple initialization loops. You would write something like
this:
result = array[
array[Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A')],
array[Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A')],
array[Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A')],
array[Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A')],
array[Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A')],
array[Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A')],
array[Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A')],
array[Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A')],
array[Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A')],
array[Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A'),Val('A')],
]
Run
> What I'm missing... a way to specify a default initializer for the Val type,
> that would be called by the compiler when creating a new variable of type
> Val, particularly for array and collections.
What you need is a default initializer for **array**. It has nothing to do with
val.
I do think Nim should auto initialize the result variable. I'm not sure if this
is a bug in the "proveInit" algorithm or a bug in the way nim is initializing
result. It may be worth a bug report but I'm not sure.
as an alternative, I wrote a macro to "initialize" arrays for you:
[https://play.nim-lang.org/#ix=23QD](https://play.nim-lang.org/#ix=23QD)