Motoko error when returning an array of variant type

The error doesn’t make sense to me. Am I missing something?

type Event = {
    #e5;
    #e20;
    #e60;
};
type ActiveEvents = [Event];

public query func activeEvents() : async ActiveEvents {
     [#e5, #e20, #e60]
};

error: parser error
┌─ /home/xps/TimeWaste/.dfx/local/canisters/backend/backend.did:49:4

49 │ e20;
│ ^^^ Unexpected token

= Expects one of “decimal”, “hex”, “id”, “text”, “}”

Error: Failed while trying to deploy canisters.
Caused by: Failed while trying to deploy canisters.
Failed to build call canisters.
Failed while trying to build all canisters.
The post-build step failed for canister ‘r7inp-6aaaa-aaaaa-aaabq-cai’ (backend) with an embedded error: Failed to build canister js for canister ‘backend’.: Candid file check failed for /home/xps/TimeWaste/.dfx/local/canisters/backend/backend.did.: Candid parser error: Unrecognized token Float("e20") found at 773:776
Expected one of “decimal”, “hex”, “id”, “text” or “}”: Unrecognized token Float("e20") found at 773:776
Expected one of “decimal”, “hex”, “id”, “text” or “}”

The error isn’t related to returning an array of variant values but rather a parser error relating to the definition of the variant type itself.

As the Float("e20") portion of the error message suggests, that part is being parsed as a floating point number.

The section on floats in the language manual says:

The ‘e’ (or ‘E’) prefixes a base 10, decimal exponent; ‘p’ (or ‘P’) prefixes a base 2, binary exponent. In both cases, the exponent is in decimal notation.

I think this should probably be considered a parser bug.

In the meantime, you can probably work around this by using something like #ev20 or #event20.

Actually, the parser errors appear to be coming from the candid parser so this may be a candid parser issue.

FYI @chenyan

The workaround I suggested should still apply.

Good catch! It’s indeed a Candid parser bug. Will be fixed the next release.

Meanwhile, you can either change the variant name as Paul suggested, or put a quote on the generated did file, e.g. variant { "e5"; "e20"; "e60" }

1 Like

Thank you for the rapid response. It makes sense now.

Glad I shared it, because it worked when I used other names for the variant, just like the suggested work arounds. But I had a feeling I should share it.