In you example, you shut up the compiler warning because you initialize 
`result` with static default values: 
    
    
    proc initGa(a: openArray[int]): Ga =
      # no warnings!
      # openArray is not compatible with array!
      # this is why result = Ga(a) does not work
      result = [Val(10), Val(10), Val(10)]   <<<< Here the compiler sees result 
as initialized
      for i in low(a) .. high(a):
        result[i] = Val(a[i])
    
    
    Run

That's the reason why the following lines can initialize the items with dynamic 
values. 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).

I know that an `openArray` in proc parameters is read only, but the nim 
compiler will copy its values to result if compatible. From the 
[documentation](https://nim-lang.org/docs/manual.html#types-open-arrays), _Any 
array with a compatible base type can be passed to an openarray parameter, the 
index type does not matter_. 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. But that's another subject of discussion...

In my code, the proc looks like: 
    
    
    type
      Val = range['A' .. 'Z']
      Board = array[1 .. 10, array[1 .. 10, Val]]
      Position = tuple[int, int]
    
    proc initBoard(pieces: seq[Position]): Board =
      # Now I must initialize result with default value, let say 'A' and
      # then fill it with the content of pieces.
      ...
    
    
    Run

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.

What I'm missing either:

  * 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.
  * a pragma that could convince the compiler that a variable is initialized.



The first way is best but I don't know if it already exists. In fact, I think 
that this problem occurs only with `array` or collections as you can always 
initialize `object` fields with the default converter.

Reply via email to