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

Added structure-valued expressions #773

Merged
merged 2 commits into from
Apr 6, 2020
Merged

Added structure-valued expressions #773

merged 2 commits into from
Apr 6, 2020

Conversation

mihaibudiu
Copy link
Contributor

This should be merged after #717

@mihaibudiu
Copy link
Contributor Author

Fixes #772

@vgurevich
Copy link
Contributor

Can we also agree that if a field is not mentioned in kvlist, it will be automatically assigned a 0 value and also agree that kvlist can be empty, meaning that:

mystruct_t mystruct = {};

will do the right thing?

@mihaibudiu
Copy link
Contributor Author

@vgurevich : There is a discussion on this topic #717
I have added this as a topic to discuss in the LDWG.

Comment on lines 3615 to 3616
| '{' kvList '}'
| typeName '{' kvList '}'
Copy link
Member

@liujed liujed Nov 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This creates the awkward situation where all of the following are valid syntax:

  • { a=0, b=1 }
  • S { a=0, b=1 }
  • (S) { a=0, b=1 }
  • (S1) S2 { a=0, b=1 }

One way around this awkwardness would be to only allow the '{' kvList '}' production and remove the typeName '{' kvList '}' production, but then typing of struct-valued expressions can become ambiguous when the program has defined multiple types that match.

Another alternative would be demote '{' kvList '}' from being a first-class expression, and only allow it as a struct initializer: S s = { a=0, b=1 };. Then, struct-valued expressions would always use the S { a=0, b=1 } form. My impression is that this would be compatible with what we released in 1.2.0, but I haven't confirmed. (This is what I was previously advocating for, but I guess that wasn't clear.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the typechecker will reject today casts with struct types.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe that's a good rule: if a cast is to a Struct or header type then it is only allowed for struct-valued expressions.

@mihaibudiu
Copy link
Contributor Author

We should try the syntax with a cast instead of a type.

@jafingerhut
Copy link
Collaborator

jafingerhut commented Mar 10, 2020

Would it make sense that if/when this is merged in, it should replace the text about structure initializers? It seems that this proposal does everything that does, and more.

Also, is it fair to say that while this proposal is not identical to "Compound literals" in C/C++, it is pretty close?

Copy link
Collaborator

@jafingerhut jafingerhut left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mihaibudiu mihaibudiu merged commit e43550a into master Apr 6, 2020
@mihaibudiu mihaibudiu deleted the structExpressions branch April 6, 2020 20:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants