solargraph icon indicating copy to clipboard operation
solargraph copied to clipboard

Factory types

Open guzart opened this issue 6 years ago • 2 comments

Hello!

Is there a way to document that a function yields an instance of the class passed in as a parameter?

module Factoryish
  def self.with_instance(some_class)
    the_instance = some_class.new
    yield the_instance
    # some more code
  end
end

Thank you for your help.

PS: Amazing, amazing work on this gem and the VS Code extension 👏 👏 👏 !

guzart avatar Jun 15 '19 22:06 guzart

Thanks!

That's not possible yet, but I might be able to make it work based on the way that return types are inferred from methods with @!macro directives. Example:

module Factoryish
  # @!macro
  #   @return [$1]
  def self.make_instance(some_class); end
end

string = Factoryish.make_instance(String)
string # <= Type is String

For a yieldparam, it should look something like this:

module Factoryish
  # @!macro
  #   @yieldparam [$1]
  def self.with_instance(some_class)
    the_instance = some_class.new
    yield the_instance
    # some more code
  end
end

Factoryish.with_instance(String) do |instance|
  instance # <= Type is String
end

I'll add it to the roadmap.

castwide avatar Jun 15 '19 23:06 castwide

When you intend to implement this feature???

minkir014 avatar Jul 26 '19 23:07 minkir014