AaronC81 / sord

Convert YARD docs to Sorbet RBI and Ruby 3/Steep RBS files
https://sord.aaronc.cc
MIT License
304 stars 18 forks source link

Resolve references to built-in RBS duck types #154

Closed mucaho closed 2 years ago

mucaho commented 2 years ago

Currently, sord converts all duck types to untyped RBS types. This makes perfect sense, as we don't know the signature of arbitrary duck types - what parameters they accept, what their types are, and what the return type is.

However, there are a couple of builtin interface type signatures in RBS, which can be considered universally valid. Would it be possible to put references to these interfaces instead of unknown types into the generated RBS signatures?

For example:

# @param input [#to_s] the input to print
# @return [void]
def print(input)
  puts input
end

would convert the input duck type #to_s to a reference to the _ToS builtin interface

def print: (_ToS input) -> void

Of course, currently, you can get a similar behavior by defining a ToS module yourself, and use that instead of #to_s

# @abstract
module ToS
  def self.included(...)
    raise "This marker interface is not meant to be included!"
  end
  def self.extended(...)
    raise "This marker interface is not meant to be extended!"
  end
  # @abstract
  # @return [String]
  def to_s()
    raise TypeError, "No implicit conversion to String possible."
  end
end

However, this introduces unnecessary clutter to your codebase.

AaronC81 commented 2 years ago

Opened a PR for this, #161! Great suggestion, thanks