Ternary operator

Does Motoko support a ternary operator?

I tried

var condition = false;
condition ? "I am true" : "I am false";

but I get

syntax error, unexpected token '?', expected one of token or <phrase> sequence: ...

If there is no ternary operator, is there a concise way to accomplish inline switching like this?

Unfortunately, there is no ternary operator. :slightly_frowning_face: …The question mark acts like a constructor for the optional type. I would love it if we added some syntactic sugar here. Are there any proposals for this @claudio ?

1 Like

if's are expressions in Motoko, so you can just write:

let result = if (false) { "I am true" } else { "I am false" };
assert(result == "I am false");