I try to write a small function which returns the find result of a trie search.
The function is working but in the next step I would like to get a property from the returned asset. If the asset exists I would like to print it out. This is working too, but if I try to print only one property or use the value of that property in an other variable then I got the following error:
main.mo:92.15-92.21: type error [M0070], expected object type, but expression produces type ?Asset
public type Asset = {
id: Nat32;
version: Nat32;
text: Text;
};
let result = Trie.find(assets, key(id), Nat32.equal);
let exists = Option.isSome(result);
if (exists) {
D.print(debug_show(result));
let a = result.version;
D.print(debug_show(a));
};
result in this line is an optional value returned from the trie (type ?Asset), not the value by itself (type Asset).
An easier approach here would be to use a switch statement, it will unwrap the value for you when it does the pattern match so you can work with it more readily, eg:
let result = Trie.find(assets, key(id), Nat32.equal);
switch(result) {
case(?exists) {
D.print(debug_show(exists.version));
};
case(null) {
// entry not found in trie
};
};