Records without named variables

I am doing intercansiter call using a candid interface and it seems like it wants me to produce a record without a named variable … I hav abstracted a use case here to define what I think I need to do?

  type MyType = {
    varName1 :Text;
    varName2 :Text;
  };

  public func showRecord() : async MyType {


    // mo code to populate:
    var myNewVar : MyType = {
      varName1 = "banana";
      varName2 = "orange";
    };

    return myNewVar ;

  } ; // end showRecord

running

dfx canister call nftmanager showRecord "()"

produces:

(record { varName1 = "banana"; varName2 = "orange" })

is there anyway to define the type without names? so I can produce :

(record {"banana"; "orange" })

the value I would submit to the other canister if I did a dfx call would not have the named values i.e

dfx canister call othercanister "(record {\"banana\"; \"orange\" })"

From the candid side, record always has field names, but if the field name is numbered consecutively starting from 0, we can omit the field name as a shorthand. So record { 0 : text; 1 : text} can be written as record { text; text }. In Motoko, this type maps to tuple. For example,

    public func test(tuple: (Text,Text)) : async ((Text, Text)) {
        tuple
    } 

The above function takes record { text; text } and returns record { text; text }

1 Like

THANK YOU … just learned something from you, so appreciate it, especially the code example …


  type MyType = (Text,Text );

  public func showRecord() : async MyType {


    // mo code to populate:
    var myNewVar : MyType = (
      "banana",
      "orange"
    );

    return myNewVar ;

  } ; // end showRecord

basically this works …
Really appreciate the quick response … got my head out the wall it was bangin’ against

It’s probably still better design to have names, though, to avoid confusing their order, make the API easier to understand etc.

The most common place to see it is in map structures. Rather than a K => V type, maps are represented in Candid as vec record { K; V; }

1 Like