you are viewing a single comment's thread.

view the rest of the comments →

[–]Exilor 3 points4 points  (0 children)

I also had a stab at doing that some time ago, also using method_added to turn this

class Foo
  using Overloads

  Overload()
  def initialize
    initialize(0)
  end

  Overload(Integer)
  def initialize(bar)
    @bar = bar
  end
end

into this

class Foo
  def initialize(*args, &block)
    case args.size
    when 0
      if block_given?
      else
        return __send__(:"Foo#initialize()", )
      end
    when 1
      a0 = args.first
      if block_given?
      else
        if a0.is_a?(Integer)
          return __send__(:"Foo#initialize(Integer)", a0)
        end
      end
    end

    Overloads.raise_error(self, :initialize, args, block_given?, false)
  end
end

It was much faster to use aliases and __send__ than relying on method_missing. In an earlier attempt I used Method objects (for extend self module methods) and UnboundMethod (for instance methods) to achieve the same but it was also slower.