In Motoko, does HashMap<Text, File> gives value or reference when doing get(Key)?

This is my defined File class as below.

public class File() {
    let chunkHashMap = HashMap.HashMap<Nat, [Nat8]>(1, Nat.equal, Int.hash);
        public func insertChunk(chunkNumber : Nat, fileChunk : [Nat8]) {
            chunkHashMap.put(chunkNumber, fileChunk);
        };
    };

Now let’s say I have a hashmap as files = HashMap<Text, File>(1, Text.equal, Text.hash).

let isSuccess = switch(files.get(fileName)) {
     case(null) return false;
     case(?file) file.insertChunk(chunkNumber, fileChunk);
};
return isSuccess;

So, does above code will update the value for the corresponding key in hash-map “files”. So my main question is does files.get(fileName) returns reference to File class object?

Whatever is more efficient, usually by reference, but you can’t tell the difference, and that’s by design. See my answers here and here.

1 Like