-
Notifications
You must be signed in to change notification settings - Fork 205
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
Zero length and unary records #2386
Comments
Unary records used to be in the proposal and I took them out. They caused some disagreement around whether a unary record is isomorphic to its field or not. Likewise, is the zero-length record I anticipate adding them in a future release when we add support for argument list reification and spreading. If you think it's worth adding them now, I think we can make it work. Python uses a trailing comma to disambiguate parenthesized expressions and one-field tuples:
It's a little funny looking, but could work. I like your suggestion of just exposing them through an API. We do have to think about pattern matching too, though. The current proposal does allow parentheses for grouping in patterns, which would be ambiguous with a unary record pattern. |
I think the answer to both of these would clearly have to be no. Life gets very squirrely if you say yes, and I see no benefit to doing so.
I think it's at least worth making it explicit that the runtime set of values should be considered to include unary and nullary records, even if we don't add syntax for introducing them (to avoid implementations building in assumptions that become problematic in the future).
This would be fine. Maybe even
If we do nothing else, I'd probably suggest doing that, at least so we can test them. Though I guess we could keep it private for now. |
See also previous discussion here. Apparently I've started to repeat myself. And say the same thing multiple times as well. |
We could just do it later: The zero and one component records could be part of a future enhancement about spreading tuples into actual argument lists, as long as we make sure those records are a syntax error. That's true for |
I don't know if this was considered but couldn't you also use types to differentiate between a parenthesized expression and a unary record? int number = (1);
(int) record = (1); A similar thing is currently done to differentiate maps and sets. Map map = {};
Set set = {}; |
Using types to differentiate between ambiguous syntactic constructs is possible, but comes with a cost of, well, ambiguity. Imagine someone wrote extension methods on tuples, like: extension Await2<S, T> on (Future<S>, Future<T>) {
Future<(S, T)> get wait =>
Future.wait([this.0, this.1]).then((list) => (list[0] as S, list[1] as T);
}
// For completeness.
extension Await1<S, T> on (Future<S>) {
Future<S> get wait => this.0;
} You'd think that At that point, you'd need a way to create a singleton tuple, and you don't have a context type, so you're stuck. (I'd still like the implicit type-based conversion, but I know other people in the language team are more wary about dding those, and with good reason.) |
If we have unary and nullary tuples, we also need to have types for them. The most consistent type syntax would probably be (I still think using |
* Address a bunch of records issues. - Support constant records. Fix #2337. - Support empty and one-positional-field records. Fix #2386. - Re-add support for positional field getters Fix #2388. - Specify the behavior of `toString()`. Fix #2389. - Disambiguate record types in `on` clauses. Fix #2406. * Clarify the iteration order of fields in `==`. * Copy-edit the sections on const records and canonicalization. There should be no meaningful changes. I just: - Fixed some misspellings. - Used Markdown style consistent with the rest of the doc. - Re-worded things to, I hope, read a little more naturally. - Removed the parenthetical on identical() in a const context because that felt a little too academic. * Leave the order that positional fields are checked in == unspecified. * Clarify that positional fields are not sugar for named fields. Specify the evaluation order of fields.
The records proposal forbids empty records and records with a single entry:
There has been discussion in the past of trying to exploit the symmetry between parameter lists and records. I'm slightly worried that this restriction may come back to bite us, since it prevents (e.g.) uniformly reifying argument lists as records. Is it possible to make this a restriction only on the literal syntax? That is, semantically we still have zero length and unary records, we just have no literal syntax for them?
As a second step (or an alternative step) we could now or in the future add alternative syntax that generalized fully. For example, I can imagine:
Record.unit
onRecord
which is the unique zero length record.Record.single
onRecord
which produces a unary tupleRecord
such thatRecord(....)
produces the record corresponding to the literal record syntax(...)
, with the additional generality that...
may be empty or a single positional argument.cc @munificent @lrhn @eernstg @natebosch @jakemac53 @stereotype441
The text was updated successfully, but these errors were encountered: