wing icon indicating copy to clipboard operation
wing copied to clipboard

Structural type checking for structs

Open MarkMcCulloh opened this issue 1 year ago • 2 comments

I tried this:

struct A {
  a: num;
}

struct B {
  a: num;
}

let x: A = B { a: 0 };

This happened:

Compilation error (Expected type to be "A", but got "B" instead)

I expected this:

No error. B structural satisfies A, which should be good enough for structs

Anything else?

This should work with the same logic as struct expansion

Community Notes

  • Please vote by adding a 👍 reaction to the issue to help us prioritize.
  • If you are interested to work on this issue, please leave a comment.

MarkMcCulloh avatar May 13 '24 13:05 MarkMcCulloh

Right now Wing treats struct types "nominatively" where the type is part of its identity, so this isn't a bug. In the language reference it mentions

Two structs are equal if they have the same type and all of their fields are equal (based on rules of equality of their type).

There are tradeoffs to both kinds of type systems

Chriscbr avatar May 13 '24 14:05 Chriscbr

Fair, forgot to reference the language ref.

There are tradeoffs to both kinds of type systems

Of course, but I feel like we've already made the choice to treat structs as structurally typed in some cases e.g. struct expansion and Json casting. No real reason to not give the same benefit to named structs as far as I can tell.

MarkMcCulloh avatar May 13 '24 14:05 MarkMcCulloh

Hi,

This issue hasn't seen activity in 90 days. Therefore, we are marking this issue as stale for now. It will be closed after 7 days. Feel free to re-open this issue when there's an update or relevant information to be added. Thanks!

github-actions[bot] avatar Aug 22 '24 06:08 github-actions[bot]

Related: https://github.com/winglang/wing/discussions/5102

Chriscbr avatar Aug 29 '24 14:08 Chriscbr