Hi,

2010/11/25 Yusuke ENDOH <mame / tsg.ne.jp>:
>> As I said at RubyConf 2010, I'd like to propose a new features called
>> "Refinements."
>
>
> Basically I agree with your proposal, but I think that some
> discussion is needed.
>
> =A0- Your patch is too big. =A0Could you separate it to some parts?
> =A0 =A0It is hard (for me) to review it.

Shyouhei suggested github, but my github account has no enough disk
space, so I put the git format-patch result at the following site:

  http://shugo.net/tmp/refinement-r29837-20101124.zip (ZIP archive)
  http://shugo.net/tmp/refinement-r29837-20101124/ (Expanded directory)

However, I'm afraid that there are too many patches, some of which
have been already reverted.

> =A0- Your patch adds `klass' to stack frame, which may cause
> =A0 =A0big performance degradation. =A0We should check benchmark
> =A0 =A0result (though I'm not so concerned).

I have run "make benchmark" 5 times, and it shows that the modified
version is slower than average 2.5% than the original version.

The benchmark result is available at:

  http://shugo.net/tmp/refinement-benchmark-20101107.csv

Additional tests are welcome.

> =A0- API design. =A0Why did you select:
>
> =A0 =A0 =A0module MathN
> =A0 =A0 =A0 =A0refine(Fixnum) do
> =A0 =A0 =A0 =A0 =A0def /(other) quo(other) end
> =A0 =A0 =A0 =A0end
> =A0 =A0 =A0end
> =A0 =A0 =A0using MathN
>
> =A0 =A0and not select others, such as:
>
> =A0 =A0 =A0module MathN
> =A0 =A0 =A0 =A0refine(Fixnum)
> =A0 =A0 =A0 =A0def /(other) quo(other) end
> =A0 =A0 =A0end
> =A0 =A0 =A0using MathN
>
> =A0 =A0or:
>
> =A0 =A0 =A0MathN =3D refine(Fixnum) do
> =A0 =A0 =A0 =A0def /(other) quo(other) end
> =A0 =A0 =A0end
> =A0 =A0 =A0using MathN

Because I'd like to allow multiple refinements in one module, for
example, in the following case:

  module MyXmlFormat
    refine Integer do
      def to_xml; ...; end
    end

    refine String do
      def to_xml; ...; end
    end

    refine Hash do
      def to_xml; ...; end
    end
    ...
  end

In this case, I prefer the above syntax to your proposals.

> =A0 =A0IMO, it will be more natural to provide this feature as new
> =A0 =A0constract with new syntax, instead of Module's methods.

First, I consider it, but I wouldn't like to introduce new keywords.

> =A0- Is it intended to reject refining module methods?
>
> =A0 =A0module ComplexExt
> =A0 =A0 =A0refine(Math) do
> =A0 =A0 =A0 =A0def sqrt(x)
> =A0 =A0 =A0 =A0 =A0(x >=3D 0 ? 1 : Complex::I) * super(x.abs)
> =A0 =A0 =A0 =A0end
> =A0 =A0 =A0end
> =A0 =A0end
> =A0 =A0#=3D> wrong argument type Module (expected Class) (TypeError)

It's limitation of the current implementation.
However, I don't think it's critical, because If you'd like to refine
a module, you can refine a class which includes the module instead.

--=20
Shugo Maeda