winglang / wing

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

Type inference of arrays doesn't allow subtypes #6170

Open eladb opened 7 months ago

eladb commented 7 months ago

I tried this:

struct Base {}
struct Derived1 extends Base {}
struct Derived2 extends Base {}

let arr: Array<Base> = [
  Derived1 {},
  Derived2 {},
];

This happened:

error: Expected type to be "Derived1", but got "Derived2" instead
  --> wing/main.w:7:3
  |
7 |   Derived2 {},
  |   ^^^^^^^^^^^ Expected type to be "Derived1", but got "Derived2" instead

I expected this:

I expected this inference to work

Is there a workaround?

Yes, be explicit about the type:

struct Base {}
struct Derived1 extends Base {}
struct Derived2 extends Base {}

let arr: Array<Base> = Array<Base>[
  Derived1 {},
  Derived2 {},
];

Anything else?

No response

Wing Version

No response

Node.js Version

No response

Platform(s)

No response

Community Notes

github-actions[bot] commented 4 months ago

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] commented 3 weeks ago

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!