Skip to content
This repository was archived by the owner on Aug 17, 2022. It is now read-only.
This repository was archived by the owner on Aug 17, 2022. It is now read-only.

Backwards compatible additions to variants #145

@badeend

Description

@badeend

Some enums are "open ended" in nature. Imagine a function that conditionally returns an enum containing the error code. For example:

(type $Errno1 (variant (case "acces") (case "badf") (case "busy")))

There is no way to add new error cases to that enum without breaking backwards compatibility, since the enum is used in a return position. Note that this problem does not occur when the enum is used as a parameter.

As it stands with the current Interface Types proposal; to work around this limitation we have to resort to returning meaningless integers (or any other type of artificial constant):

const Errno1_access: i32 = 1;
const Errno1_badf: i32 = 2;
const Errno1_busy: i32 = 3;

I suggest adding some kind of annotation to variant types that optionally marks a single case as the fallback that future additions resolve to. For example:

(type $Errno2 (variant (case "acces") (case "badf") (case "busy") (case "unknown") fallback "unknown"))
(type $Errno3 (variant (case "acces") (case "badf") (case "busy") (case "eagain") (case "unknown") fallback "unknown"))

Because $Errno2 has a fallback case, $Errno3 can now be coerced into $Errno2. All cases of $Errno3 not present in $Errno2 will be coerced to $Errno2s fallback case: "unknown". This does mean data might be discarded during coercion, however I'd argue this is OK since you'd have to explicitly op-in to this behavior in the original type.

Restrictions:

  • Only constructor-less cases can be marked as fallback, ie cases that carry no additional data.
  • $Errno3 must be a superset of $Errno2.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions