Help Needed: Motoko Compilation Error - M0116
Hey everyone! Working on a project and stuck on a persistent Motoko compilation error. Any help would be greatly appreciated!
Error:
variant pattern cannot consume expected type Principal Motoko(M0116)
Code causing the issue:
private func generateMockCanisterId() : Principal {
let mockId = "rdmx6-jaaaa-aaaah-qcaiq-cai";
switch (Principal.fromText(mockId)) {
case (#ok principal) principal;
case (#err _) Principal.fromBlob(Blob.fromArray([1, 2, 3, 4, 5]));
}
};
What we’ve tried:
Fixed pattern matching syntax (removed parentheses)
Updated dfx.json configuration
Verified file structure and imports
Tested with both
#ok(principal)
and #ok principal
patterns
Checked Result type usage (lowercase #ok/#err vs uppercase #Ok/#Err)
Project Context:
- DFX version: 0.26.1
- Building a Motoko backend canister
- Error occurs during
dfx build autopilot_backend
- Files are properly structured in
src/autopilot-icp-backend/
The error specifically points to the Principal destructuring in the pattern match. Is there something specific about Principal type handling in pattern matching that we’re missing?
Full error output:
/path/to/deploy.mo:70.23-70.37: type error [M0116], variant pattern cannot consume expected type Principal
Any insights on what might be causing this? Thanks in advance!