Array : cannot implicitly instantiate

the code is :

var buffer : [var (Nat, Nat)] = [var];
buffer := Array.append(buffer, [var (1,1)]);

and I meet the error:

type error [M0098], cannot implicitly instantiate function of type
  <A>([A], [A]) -> [A]
to argument of type
  ([var (Nat, Nat)], [var (Nat, Nat)])
to produce result of type
  [var (Nat, Nat)]
because no instantiation of A/73 makes
  ([var (Nat, Nat)], [var (Nat, Nat)])  <:  ([A/73], [A/73])
and
  [A/73]  <:  [var (Nat, Nat)]

I have known that the error can be handled by add the <T> after the Array.append function if the array is a immutable array, but here buffer is a mutable array

Array.append only works on immutable arrays (the ones without var), and mutable arrays (the ones with var) are a completely separate type. You could implement your own append_var function, but really, when you are appending arrays, you are probably using the wrong type. Have a look at the buffer type in the base library!

3 Likes

Some reference for the buffer module in the base library for you: motoko-base/Buffer.mo at master · dfinity/motoko-base · GitHub

2 Likes

Thank you, here buffer is not the real ‘Buffer’ but a temp array. I have realized the append function for [var T], thank you nomeata :slight_smile: