ruby / rbs

Type Signature for Ruby
Other
1.94k stars 212 forks source link

Handling Classes created with Data#define #1563

Open Forthoney opened 11 months ago

Forthoney commented 11 months ago

The dynamic nature of defining Data classes with Data#define makes it difficult (if not impossible) to adequately describe scripts using RBS. If it indeed is impossible, perhaps we should consider a specific syntax for Data classes in RBS. Yes, they are dynamically created classes, so checking is tough, but it is odd to have a first class, core library feature not supported by RBS. If it is possible and I simply did not know, perhaps updating the tutorial to better discuss how to do this would be very helpful

ksss commented 11 months ago

I have an idea. How about generating prototype RBS when rbs prototype runtime detects Struct and Data? (It's not implemented yet.) Would this be of help to you?

For example:

# foo.rb
class Foo < Data.define(:foo, :bar)
end

$ rbs prototype runtime -R foo.rb Foo

# foo.rbs
class Foo < Data
  def initialize: (untyped foo, untyped bar) -> void
                | (foo: untyped, bar: untyped) -> void
  attr_reader foo: untyped
  attr_reader bar: untyped
end