winglang / wing

A programming language for the cloud ☁️ A unified programming model, combining infrastructure and runtime code into one language ⚡
https://winglang.io
Other
4.79k stars 189 forks source link

Structural type checking for structs #6467

Open MarkMcCulloh opened 2 months ago

MarkMcCulloh commented 2 months ago

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

Chriscbr commented 2 months ago

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

MarkMcCulloh commented 2 months ago

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.