Motoko things I learned this week 🤦‍♂️ debug and setting via pattern matching

debug { D.print("a thing i need to debug")};

The above gets taken out of your code by the compiler at build time if you use the release flag. The Motoko team has the flags, so I’ll let them document, but this enables a much better local development debug pattern like:

let debug_channel = {
   feature1 = true;
   feature2 = false;
};

...later

public shared func test() : async Bool{
    debug{ if(debug_channel.feature1){D.print("my function was called");};
};

Also only for local debugging because it will trap, but a godsend for testing:

   let #ok(myresult) = await myservice.function(); //throws if not an #ok variant
   let something = myresult.without_unwrapping;

Again…don’t use this in your main actor code, but if you are writing motoko tests, this is going to save me hundreds of lines of code.

3 Likes

Drive-by: in fact, you can pattern-match this in one line:

let #ok({without_wrapping = something}) = await myservice.function();

Or, if you don’t need to rename the field:

let #ok({without_wrapping}) = await myservice.function();
4 Likes

There has always been some basic doc here

But we should add something more prominent elsewhere.