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

Better support for inherited class of Struct or Data by prototype runtime #1571

Merged
merged 2 commits into from Oct 30, 2023

Conversation

ksss
Copy link
Collaborator

@ksss ksss commented Oct 17, 2023

When running rbs prototype runtime, I've made special branches for classes that inherit from Struct and Data, allowing for the generation of more user-friendly RBS as prototypes.

Demo

# t.rb
class Foo < Struct.new(:foo, :bar, keyword_init: true)
end

Bar = Struct.new(:foo, :bar, keyword_init: true)

class Baz < Data.define(:foo, :bar)
end

Qux = Data.define(:foo, :bar)
$ bundle exec rbs prototype runtime -R t.rb Foo Bar Baz Qux

before

class Bar < ::Struct[untyped]
  def self.[]: (*untyped) -> untyped

  def self.inspect: () -> untyped

  def self.keyword_init?: () -> untyped

  def self.members: () -> untyped

  def self.new: (*untyped) -> untyped

  public

  def bar: () -> untyped

  def bar=: (untyped _) -> untyped

  def foo: () -> untyped

  def foo=: (untyped _) -> untyped
end

class Baz
end

class Foo
end

class Qux < ::Data
  def self.[]: (*untyped) -> untyped

  def self.inspect: () -> untyped

  def self.members: () -> untyped

  def self.new: (*untyped) -> untyped

  public

  def bar: () -> untyped

  def foo: () -> untyped
end

after

class Bar < ::Struct[untyped]
  def self.new: (?foo: untyped, ?bar: untyped) -> instance

  def self.[]: (?foo: untyped, ?bar: untyped) -> instance

  def self.keyword_init?: () -> true

  def self.members: () -> [ :foo, :bar ]

  def members: () -> [ :foo, :bar ]

  attr_accessor foo: untyped

  attr_accessor bar: untyped
end

class Baz < ::Data
  def self.new: (untyped foo, untyped bar) -> instance
              | (foo: untyped, bar: untyped) -> instance

  def self.[]: (untyped foo, untyped bar) -> instance
             | (foo: untyped, bar: untyped) -> instance

  def self.members: () -> [ :foo, :bar ]

  def members: () -> [ :foo, :bar ]

  attr_reader foo: untyped

  attr_reader bar: untyped
end

class Foo < ::Struct[untyped]
  def self.new: (?foo: untyped, ?bar: untyped) -> instance

  def self.[]: (?foo: untyped, ?bar: untyped) -> instance

  def self.keyword_init?: () -> true

  def self.members: () -> [ :foo, :bar ]

  def members: () -> [ :foo, :bar ]

  attr_accessor foo: untyped

  attr_accessor bar: untyped
end

class Qux < ::Data
  def self.new: (untyped foo, untyped bar) -> instance
              | (foo: untyped, bar: untyped) -> instance

  def self.[]: (untyped foo, untyped bar) -> instance
             | (foo: untyped, bar: untyped) -> instance

  def self.members: () -> [ :foo, :bar ]

  def members: () -> [ :foo, :bar ]

  attr_reader foo: untyped

  attr_reader bar: untyped
end

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.

Thank you so much!
This will be one of the most important improvements in RBS 3.3. 🎉

@soutaro soutaro added this pull request to the merge queue Oct 30, 2023
@soutaro soutaro added this to the RBS 3.3 milestone Oct 30, 2023
Merged via the queue into ruby:master with commit 1ae8980 Oct 30, 2023
23 checks passed
@ksss ksss deleted the struct-data branch October 30, 2023 06:21
@soutaro soutaro added the Released PRs already included in the released version label Oct 31, 2023
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