-
Notifications
You must be signed in to change notification settings - Fork 201
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
Comments
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 😍 |
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 The difficulty with allowing it in possible-expression spots is that it would require us to disambiguate |
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. |
Use Case
Currently, we can't define structs like this:
The workaround is to define every nested struct separately, and name every single one of them! Not the best UX out there.
Proposed Solution
Allow nesting anonymous structs:
But also allow anonymous struts like this one:
Implementation Notes
No response
Component
Language Design
Community Notes
The text was updated successfully, but these errors were encountered: