Type error [M0029], unbound type Array

Hello all,

I’m learning Motoko and I have this simple function

func shuffle(arr:Array) {}

which returns

type error [M0029], unbound type Array

Can you tell me what am I doing wrong?

1 Like

You’ll need to add an import for the Array module at the top of your file, this pulls the type in from the base library:
import Array “mo:base/Array”;

1 Like

thank you for the answer. I did this and I got the same error. I’m using 0.6.21

import Array "mo:base/Array";


   actor {

    func shuffle(arr:Array) {
    
   }

}

Ah, apologies, you’ll want to use brackets for array types, so for an array that stores type Nat you’d use:
func shuffle(arr: [Nat]) {...

You can see some more examples of array usage here: https://sdk.dfinity.org/docs/language-guide/mutable-state.html#immutable-arrays

Ah, I see. The type system is very close to Typescript.

This works for me,

func shuffle<T>(arr:[T]) :[T]{
    return arr;
};

Next challenge, find the length of an array!

arr.size() will do the trick.

ah perfect ty! By the way, it is missing from the docs Array :: Internet Computer

you can track it here:

1 Like

would it be the same syntax for index?

I am getting

type error [M0029], unbound type index