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

Anonymous structs #5731

Open
skyrpex opened this issue Feb 20, 2024 · 4 comments
Open

Anonymous structs #5731

skyrpex opened this issue Feb 20, 2024 · 4 comments
Labels
✨ enhancement New feature or request 📐 language-design Language architecture needs-discussion Further discussion is needed prior to impl

Comments

@skyrpex
Copy link
Contributor

skyrpex commented Feb 20, 2024

Use Case

Currently, we can't define structs like this:

struct Props {
  credentials: {
    clientId: str;
    clientSecret: str;
  };
}

The workaround is to define every nested struct separately, and name every single one of them! Not the best UX out there.

struct Credentials {
  clientId: str;
  clientSecret: str;
}

struct Props {
  credentials: Credentials;
}

Proposed Solution

Allow nesting anonymous structs:

struct Props {
  credentials: {
    clientId: str;
    clientSecret: str;
  };
}

But also allow anonymous struts like this one:

let person = { name: str; last: str }.tryParseJson(req.body);
log(person.name);
log(person.last);

Implementation Notes

No response

Component

Language Design

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.
  • If this issue is labeled needs-discussion, it means the spec has not been finalized yet. Please reach out on the #dev channel in the Wing Slack.
@skyrpex skyrpex added ✨ enhancement New feature or request needs-discussion Further discussion is needed prior to impl labels Feb 20, 2024
@monadabot monadabot added this to Wing Feb 20, 2024
@github-project-automation github-project-automation bot moved this to 🆕 New - not properly defined in Wing Feb 20, 2024
@eladb
Copy link
Contributor

eladb commented Feb 20, 2024

Would it be be useful to be able to also define anonymous structs even if they are not nested?

let person = { name: str; last: str }.tryParseJson(req.body);
log(person.name);
log(person.last);

@skyrpex
Copy link
Contributor Author

skyrpex commented Feb 20, 2024

Would it be be useful to be able to also define anonymous structs even if they are not nested?

let person = { name: str; last: str }.tryParseJson(req.body);

log(person.name);

log(person.last);

Sure it would 😍

@skyrpex skyrpex changed the title Anonymous nested structs Anonymous structs Feb 20, 2024
@MarkMcCulloh
Copy link
Contributor

let person = { name: str; last: str }.tryParseJson(req.body);

I also think this would be neat, but it's probably worth splitting off into a different issue due to the implementation challenges. Anon structs in unambiguous type areas would be a good/easier start (e.g. also allowing Array<{ name: str; last: str }>).

The difficulty with allowing it in possible-expression spots is that it would require us to disambiguate { name: str; last: str } as the parser would see this as a JSON literal expression.

@Chriscbr
Copy link
Contributor

Several other languages also support anonymous classes and anonymous structs, so I can see the appeal.

Stylistically, my preference is to see types named when possible since names can serve documentation and can encourage more readable code (and more readable error messages). It's related to why I find this:

let total = map(Array<Student>.fromJson(Json.parse(body)), (s) => s.name).sorted();

a bit less readable than

let data = Json.parse(body);
let students = Array<Student>.fromJson(data);
let studentNames = map(students, (s) => s.name);
studentNames.sort();

But this is mostly personal taste, I'll admit.

IMO it could be beneficial to revisit this after we address some of the other issues that are functionally limiting structs in Wing (like #3686 and #1796), since it could be trickier implement these if we add anonymous structs first.

@staycoolcall911 staycoolcall911 added the 📐 language-design Language architecture label Mar 3, 2024
@staycoolcall911 staycoolcall911 moved this from 🆕 New - not properly defined to 🤝 Backlog - handoff to owners in Wing Mar 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✨ enhancement New feature or request 📐 language-design Language architecture needs-discussion Further discussion is needed prior to impl
Projects
Status: 🤝 Backlog - handoff to owners
Development

No branches or pull requests

5 participants