Passing Variant as Argument to a function using Motoko

I’ve seen solutions for calling functions that require a variant as an argument using JS but wasn’t able to solve this using Motoko. Let’s say I have a canister called RemoteCanister that exposes a function testVariant which requires an argument of a custom type Request as defined here:

  // Types
  type MyType = {
    #one : Text;
    #two : Nat;
  };

  type Request = {
    choose : MyType;
    headline: Text;
  };

  // Function
  public shared(msg) func testVariant(data : Request) : async Text {
    return "success!";
  };

I want to call this function from another canister. How would I pass the argument for type MyType when this is a variant? This is the function call in the second canister:

  public shared(msg) func callTestVariant() : async Text {
    let response : Text = await RemoteActor.testVariant(#two: 12, "my string");
    return "yeahcool!";
  };

I’ve also tried other ways like ({two: 12}, "my string"}) or simply (12, "my string") hoping it would infer the type automatically. How does one do this?

The invocation would be

RemoteActor.testVariant(#two(12), "my string");
1 Like

Beautiful! Thank you!

Actually, it’s await RemoteActor.testVariant({choose = #two(12); headline = "my string"}); in this specific case because of the subtypes, I guess.