From: Joshua Ballanco Date: 2011-10-03T16:25:14+09:00 Subject: [ruby-core:39855] Modules as library forms --4e896351_19a52566_4992 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Hello ruby-core, =20 =46irst, thank you to everyone involved in RubyConf=21 I had a number of = good discussions there, and coming from these I would like to humbly prop= ose a potential solution to some of the issues surrounding Modules. What = I would propose would be to treat Modules (and subsequently Classes) as =22= library forms=22, and to introduce methods Module=23only, Module=23except= , Module=23prefix, and Module=23rename. These names are not arbitrary, an= d in fact correlate directly with the methods described for library forms= in Scheme's R6RS standard (section 7.1, http://www.r6rs.org/final/html/r= 6rs/r6rs-Z-H-10.html=23node=5Fsec=5F7.1). Module=23prefix and Module=23re= name are straight-forward, as their names imply. Module=23only takes a list of symbols and returns a Module containing onl= y the matching methods. Module=23except takes either another Module or a = list of identifiers and would return a Module refined according to the ar= gument. So, for example: module A def foo; end def bar; end end module B include A.only(:foo) def baz; end end =23=3D> module B now contains methods :foo and :baz, but not :bar These could also be used in combination with shadowing bindings to accomp= lish much of what refinements intended. =46or example: module A; end module B; end class String include A include B end =23 later=E2=80=A6 class =46oo String =3D String.except(A) =23=3D> from this point on, any resolution of =22String=22 in this scope= would =23 use the =22shadow=22 String class which contains methods from =23 module B but not any from module A. end I think this should also remove the performance concerns from refinements= , as nothing has changed regarding scoping rules or constant look-up. Ins= tead, there would be two =22String=22 classes now: =46oo::String and ::St= ring. Thoughts=3F - Josh =20 --4e896351_19a52566_4992 Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline
Hello ruby-core,

=46irst, thank you to everyone = involved in RubyConf=21 I had a number of good discussions there, and com= ing from these I would like to humbly propose a potential solution to som= e of the issues surrounding Modules. What I would propose would be to tre= at Modules (and subsequently Classes) as =22library forms=22, and to intr= oduce methods Module=23only, Module=23except, Module=23prefix, and Module= =23rename. These names are not arbitrary, and in fact correlate directly = with the methods described for library forms in Scheme's R6RS standard (s= ection 7.1, http://www.r6rs.org/final/html/r6rs/r6rs-= Z-H-10.html=23node=5Fsec=5F7.1). Module=23prefix and Module=23rename = are straight-forward, as their names imply.

Modu= le=23only takes a list of symbols and returns a Module containing only th= e matching methods. Module=23except takes either another Module or a= list of identifiers and would return a Module refined according to the a= rgument. So, for example:

    module A=
      def foo; end
    &nbs= p; def bar; end
    end
    modul= e B
      include A.only(:foo)
  =     def baz; end
    end
  &= nbsp; =23=3D> module B now contains methods :foo and :baz, but not :ba= r

These could also be used in combination with s= hadowing bindings to accomplish much of what refinements intended. =46or = example:

    module A; end
&= nbsp;   module B; end
    class String
      include A
      include B<= /div>
    end
    
 = ;   =23 later=E2=80=A6
    class =46oo
      String =3D String.except(A)
   =   =23=3D> from this point on, any resolution of =22String=22 in = this scope would
      =23 use the =22shadow=22 = String class which contains methods from
      =23= module B but not any from module A.
    end

I think this should also remove the performance concerns= from refinements, as nothing has changed regarding scoping rules or cons= tant look-up. Instead, there would be two =22String=22 classes now: =46oo= ::String and ::String. Thoughts=3F

- Josh
--4e896351_19a52566_4992--