Skip to content

Type Switch Operator #113

Description

@DavePearce

The type switch operator is inspired by the ? operator in Rust. Some points:

  • The ? operator is inextricably linked with std::error::Error and is used for error propagation.
  • For Whiley, a more general version of it is desirable.

In particular, since we have union types in Whiley, the ? operator should work with them. For example:

function test() -> (int|bool r):
   int x = other()?
   return x + 1

function other() -> (int|bool r):
   1

What's happening here is that, by exploiting forward type information from x the ? operator attempts to resolve by pulling out the desired item, otherwise return what is left from the enclosing function. For that to type check, what's left must fit into the return type.

This then allows us to encode exceptional control flow roughly as is done in Rust.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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