Upgrading a stable variable to a broader type

Can a stable variable of type A or B be safely upgraded to the type A or B or C?

You can safely upgrade to a supertype, and X or Y, if legal, always is a supertype of both X and Y.

@claudio will this be supported going forwards if Motoko is moving away from candid serialization?

(Pretty sure it won’t be, but I just wanted to post here for posterity’s sake).

Yes, it will be. Anything that passed the previous dfx compatibility check should be accepted.

In particular, if a stable variable is retained it can only evolve to a Motoko supertype (so the new version can consume the current value of the old version). For example, if the field has a record type, it can lose fields or weaken the type of retained fields. Similary, if the field has a variant type, it can add a new variant, or weaken the type of an existing variant.

1 Like