lsegal / yard-types-parser

Parses YARD formal type declarations into a human readable format.
http://yardoc.org/types
MIT License
9 stars 0 forks source link

Meta-type for self.class? #5

Open dmolesUC opened 3 years ago

dmolesUC commented 3 years ago

I have a mixin module where the default implementation of a certain method returns self, and classes that include the module that need to override the method are supposed to return an instance of their own type. Something like:

module Cleanable
  # return [Cleanable] of the same type as this Cleanable, but clean
  def cleaned
    self
  end

  def empty?
    false
  end

  # clean a list of [Cleanables]
  def clean_all(cleanables)
    cleanables.map(&:cleaned).reject(&:empty?)
  end
end

class CleanableLeaf
  include Cleanable
  # uses default implementation
end

class CleanableBranch
  include Cleanable

  # return [CleanableBranch] a copy of this node with clean children
  def cleaned
    CleanableBranch.new(children: clean_all(self.children)
  end

  def empty?
    children.empty?
  end
end

A similar case would be something like Numeric#abs, which returns an Integer for Integers, a Float for Floats, a Rational for Rationals, etc.

Is there a way to document this in YARD, or is returning the mixin type the best I can do?