Skip to content

Support positional destructuring of structs #202

Description

@timfennis

Structs (#197) can currently only be read field-by-field. Positional destructuring in declaration order — mirroring the positional constructor — would allow:

struct Foo { a: Int, b: Int }
let f = Foo(1, 2);
let x, y = f;
assert_eq(x, 1);
assert_eq(y, 2);

Today both paths reject this cleanly:

  • statically known type: error[resolver]: Invalid unpacking of Foo
  • dynamically typed receiver: error[vm]: cannot unpack: expected a list, tuple, or string

Unpacking is currently defined only for lists, tuples, and strings.

Design questions

  • Should struct unpacking also work in for loops and other destructuring positions?
  • Is partial unpacking (let x, y = f on a 3-field struct) an error, like it presumably is for tuples?
  • Should the analyser require the arity to match the field count at compile time when the struct type is known?

🤖

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions