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

Records: expose named/optional fields through an extension getter instead of a static method #1275

Closed
jakemac53 opened this issue Oct 30, 2020 · 4 comments
Labels
patterns Issues related to pattern matching.

Comments

@jakemac53
Copy link
Contributor

jakemac53 commented Oct 30, 2020

This was probably considered, but it might be nice to expose these static methods:

abstract class Record {
  static Iterable<Object?> positionalFields(Record record);
  static Map<Symbol, Object?> namedFields(Record record);
}

Instead as an extension on the Record class:

extension RecordInfo on Record {
  external Iterable<Object?> get positionalFields;
  external Map<Symbol, Object?> get namedFields;
}

This would be easier use, myRecord.positionalFields vs Record.positionalFields(myRecord), and it would prevent grabbing a tearoff of these methods.

One bonus of preventing a tearoff is that the compilers should be able to easily know exactly where and how these getters are invoked (and on what record types), and potentially optimize/treeshake better with that knowledge.

@jakemac53 jakemac53 added the patterns Issues related to pattern matching. label Oct 30, 2020
@jakemac53 jakemac53 changed the title Records: expose named/optional params through extension an method instead of a static method Records: expose named/optional fields through extension an method instead of a static method Oct 30, 2020
@jakemac53 jakemac53 changed the title Records: expose named/optional fields through extension an method instead of a static method Records: expose named/optional fields through an extension method instead of a static method Oct 30, 2020
@jakemac53 jakemac53 changed the title Records: expose named/optional fields through an extension method instead of a static method Records: expose named/optional fields through an extension getter instead of a static method Oct 30, 2020
@munificent
Copy link
Member

This would be easier use, myRecord.positionalFields vs Record.positionalFields(myRecord), and it would prevent grabbing a tearoff of these methods.

You can tear off extension methods too, so I don't think this would buy us anything.

@jakemac53
Copy link
Contributor Author

jakemac53 commented Nov 3, 2020

You can tear off extension methods too, so I don't think this would buy us anything.

But we can make it a getter, which afaik you can't tear off? (sorry I wasn't super clear about that part)

@munificent
Copy link
Member

Ah, right. I could be wrong, but I don't think that buys us much in terms of optimization affordances.

@jakemac53
Copy link
Contributor Author

The thinking is that we would know exactly where all the invocations of this method are, and could potentially optimize away all the metadata required to power this feature when it isn't used.

You could still definitely circumvent this though by calling these getters on something typed Record or dynamic etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
patterns Issues related to pattern matching.
Projects
None yet
Development

No branches or pull requests

2 participants