How to work with type record

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));
        };

Thanks for any help to get over this step.

:grinning: interesting… love to know the right answer. haven’t tried.

maybe you could return “?Asset” and do the if condition and print from the calling function?

let a = result.version;

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
    };
};
1 Like

Thanks for the hint, but I can not assign the exists.version property to an existing variable.

version = exists.version;
leads to a syntax error:
syntax error [M0001], unexpected token ‘=’, expected one of token or sequence

   public func storeAsset(id: Nat32, text: Text) : async ArtId {
    let assetKey = next;
    next += 1;
    
    let version:Nat32 = 1;

    let result = Trie.find(assets, key(id), Nat32.equal);
    let exists = Option.isSome(result);
    switch(result) {
      case(?exists) {
        version = exists.version;
        D.print(debug_show(exists.version));
        D.print(debug_show(version));

      };
      case(null) {
        // entry not found in trie  
      };
    };

    let asset : Asset = {
      id = id; 
      version = version;
      text = text;
    };
    assets := Trie.replace(
      assets,
      key(assetKey),
      Nat32.equal,
      ?asset,
    ).0;
    return assetKey;
  };
1 Like

let bindings aren’t mutable/updateable, but var bindings are. So either change to:

var version : Nat32 = 1;  // use var for a mutable/updatable variable
...
... version := exists.version; // use := for assignment

(and use := for assignment/update).

Or use a second let in the case:

      case(?exists) {
        let version = exists.version; // inserted let
        D.print(debug_show(exists.version));
        D.print(debug_show(version));

      };

Hope that helps!
Claudio

thank you so much, The let keyword was the problem.