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

parser: enable record types with optional fields #1717

Merged
merged 1 commit into from
Jan 18, 2024

Conversation

HoneyryderChuck
Copy link
Contributor

this enables richer definitions of record types, which can only be defined nowadays with Hash[Symbol, untyped].

To handle #504

@HoneyryderChuck
Copy link
Contributor Author

@soutaro this is a draft proposal, as in, the optional fields are parsed and stored, but not used yet. If you agree with this, I can proceed and use it in inferences (may need some help with that eventually as well).

@soutaro soutaro added this to the RBS 3.5 milestone Jan 12, 2024
@soutaro
Copy link
Member

soutaro commented Jan 12, 2024

@HoneyryderChuck Thanks! Let's go with the syntax.

Having different hash tables for required and optional fields would result breaking the order of the records, for Writer or rewriting features. I'd like to add #all_fields attribute and let #fields and #optional_fields fields filtered from #all_fields.

    class Record
      attr_reader fields: Hash[Symbol, [t, bool]]

      type loc = Location[bot, bot]

      def initialize: (fields: Hash[Symbol, t], location: loc?) -> void      # For compatibility with 3.4
                          | (all_fields: Hash[Symbol, [t, bool]], location: loc?) -> void

      include _TypeBase

      def fields: () -> Hash[Symbol, t]
      def optional_fields: () -> Hash[Symbol, t]?
    end

@HoneyryderChuck
Copy link
Contributor Author

@soutaro can you check the changes? I believe this is what you were aiming for?

@soutaro
Copy link
Member

soutaro commented Jan 15, 2024

Thanks @HoneyryderChuck! Looks great. Do you think it's ready to merge?

ext/rbs_extension/parser.c Outdated Show resolved Hide resolved

include _TypeBase

attr_reader location: loc?

def fields: () -> Hash[Symbol, t]

def optional_fields: () -> Hash[Symbol, t]?
Copy link
Member

Choose a reason for hiding this comment

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

Ah, I made a mistake to put ? at the end of the method type. It should return an empty hash.

Suggested change
def optional_fields: () -> Hash[Symbol, t]?
def optional_fields: () -> Hash[Symbol, t]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

if you look at the implementation, nothing is returned if all_fields is the same as fields. I think that's fine, i.e. we can avoid creating that extra hash when not necessary. Or do you see any use for it?

this enables richer definitions of record types, which can only be defined nowadays with Hash[Symbol, untyped].
@HoneyryderChuck
Copy link
Contributor Author

Do you think it's ready to merge?

It depends: the optional field set is not yet being used in inferences. Do you think that's a separate change? If so, I'd say yes, it's ready to merge.

Separate change or not, will you do it, or shall I perhaps try it out? (pointers on what/where definitely welcome if so 🙏 )

@HoneyryderChuck
Copy link
Contributor Author

@soutaro anything on the above?

Copy link
Member

@soutaro soutaro left a comment

Choose a reason for hiding this comment

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

Thanks! 👍

@soutaro soutaro added this pull request to the merge queue Jan 18, 2024
Merged via the queue into ruby:master with commit 5d1285e Jan 18, 2024
15 checks passed
@HoneyryderChuck HoneyryderChuck deleted the issue-504 branch January 18, 2024 07:55
@soutaro soutaro added the Released PRs already included in the released version label Jun 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Released PRs already included in the released version
Development

Successfully merging this pull request may close these issues.

None yet

2 participants