Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expected type to be "Array<Json>", but got "Array<Json>" instead #5421

Closed
Chriscbr opened this issue Jan 4, 2024 · 4 comments · Fixed by #6999
Closed

Expected type to be "Array<Json>", but got "Array<Json>" instead #5421

Chriscbr opened this issue Jan 4, 2024 · 4 comments · Fixed by #6999
Assignees
Labels
🐛 bug Something isn't working 🛠️ compiler Compiler

Comments

@Chriscbr
Copy link
Contributor

Chriscbr commented Jan 4, 2024

I tried this:

let extra = MutArray<Json>["bar"];
let base = [Json "foo"];
base.concat(extra.copy());

This happened:

error: Expected type to be "Array<Json>", but got "Array<Json>" instead
  --> main.w:3:13
  |
3 | base.concat(extra.copy());
  |              ^^^^^^^^^^ Expected type to be "Array<Json>", but got "Array<Json>" instead

I expected this:

No error

Is there a workaround?

If I change the second statement to

let base = Array<Json>["foo"];

then it compiles.

Anything else?

No response

Wing Version

0.54.28

Node.js Version

20.9.0

Platform(s)

MacOS

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.
@Chriscbr Chriscbr added 🐛 bug Something isn't working 🛠️ compiler Compiler labels Jan 4, 2024
@Chriscbr
Copy link
Contributor Author

Chriscbr commented Jan 4, 2024

The error is similar the one in #3822, but it might not be the same cause

@Chriscbr
Copy link
Contributor Author

Chriscbr commented Jan 4, 2024

Another variation:

let base = Array<Json>["foo"];

let obj = {
  first: "ok",
  second: ["cool"].concat(base)
};

Here's the error it produces today

error: Expected type to be "Array<Json>", but got "Array<Json>" instead
  --> main.w:5:27
  |
5 |   second: ["cool"].concat(base)
  |                           ^^^^ Expected type to be "Array<Json>", but got "Array<Json>" instead

In principle this code should also compile if base is annotated as Array<str>.

@Chriscbr Chriscbr added this to the Winglang Stable Release milestone Feb 29, 2024
@Chriscbr
Copy link
Contributor Author

Chriscbr commented Jul 30, 2024

Another example

let arr = MutArray<num>[1, 2, 3];
arr.push(4);

let takeJson = (obj: Json) => {
  // ...
};

takeJson({ values: arr });

yields:

Failed to compile.

error: Expected type to be "Json", but got "Json" instead
  --> wing/main.w:8:10
  |
8 | takeJson({ values: arr });
  |          ^^^^^^^^^^^^^^^


error: "MutArray<num>" is not a legal JSON value
  --> wing/main.w:8:20
  |
8 | takeJson({ values: arr });
  |                    ^^^

The easiest fix is to convert the MutArray to a plain Array by calling .copy(), but the error message should be improved

@monadabot
Copy link
Contributor

Congrats! 🚀 This was released in Wing 0.82.3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 bug Something isn't working 🛠️ compiler Compiler
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants