From: "Юрий Соколов" Date: 2013-02-24T02:12:17+09:00 Subject: [ruby-core:52746] Re: [ruby-trunk - Feature #7914] Case for local class methods --f46d044306aa1880dc04d6676835 Content-Type: text/plain; charset=KOI8-R Content-Transfer-Encoding: quoted-printable Thomas Sawyer, you are the language troll, IMHO. (But, maybe I'm too) Everyone else, excuse me for not being polite. 23.02.2013 12:17 =D0=CF=CC=D8=DA=CF=D7=C1=D4=C5=CC=D8 "trans (Thomas Sawyer= )" =CE=C1=D0=C9=D3=C1=CC: > > Issue #7914 has been updated by trans (Thomas Sawyer). > > > @rosenfeld Maybe I approached this backwards. I just wanted to show one > possible use case for supporting local vs. non-local class methods. Your > in-method conditional solution works for this specific case, true. But ho= w > well does it translate to other cases? For instance, it would not work wi= th > anonymous classes. called_by_super is an interesting notion, but after so= me > thought it feels like a make shift approach that only address part of the > wider issue. The module non-local side of this is important too. And its > use case is much more obvious --everywhere the included/ClassMethods hack > is used. > > > > > ---------------------------------------- > Feature #7914: Case for local class methods > https://bugs.ruby-lang.org/issues/7914#change-36827 > > Author: trans (Thomas Sawyer) > Status: Open > Priority: Normal > Assignee: matz (Yukihiro Matsumoto) > Category: core > Target version: next minor > > > =3Dbegin > Here is a use case for local class methods. > > Say we wish to give certain classes and all subclasses a special name. > > class X > def self.special_name > "special:#{name}" > end > end > class Y < X; end > class Z < Y; end > > Z.special_name #=3D> "special:Z" > > But what if Y has a unique special name? > > class Y < X > def special_name > 'unique:Y' > end > end > > Problem that arises: > > Z.special_name #=3D> "unique:Y" # wrong! > > Currently, to solve this would require creating an additional method, e.g= . > `unique_name` and redefine `special_name` to first look for unique_name > then fallback to default special name if non-found. It works, but adds > additional complexity to API. > > Nicer solution would be local class methods. > > class Y < X > def special_name > 'unique:Y' > end > local :special_name > end > > Y.special_name #=3D> "unique:Y" > Z.special_name #=3D> "special:Z" > > The idea being that local class methods are skipped in super/lookup chain= . > > This idea is not without precedence. Module class methods can be thought > of as being local. So this idea has other side of the notion, that module= s > could have class methods that are not skipped over in the super/lookup > chain. In that case we would need a term that means opposite of local, so > I'll use `nonlocal`: > > module M > def self.q; "q"; end > nonlocal :q > end > > class X > include M > end > > X.q #=3D> "q" > > =3Dend > > > > -- > http://bugs.ruby-lang.org/ > > --f46d044306aa1880dc04d6676835 Content-Type: text/html; charset=KOI8-R Content-Transfer-Encoding: quoted-printable

Thomas Sawyer, you are the language troll, IMHO.

(But, maybe I'm too)

Everyone else, excuse me for not being polite.

23.02.2013 12:17 =D0=CF=CC=D8=DA=CF=D7=C1=D4=C5= =CC=D8 "trans (Thomas Sawyer)" <transfire@gmail.com> =CE=C1=D0=C9=D3=C1=CC:

Issue #7914 has been updated by trans (Thomas Sawyer).


@rosenfeld Maybe I approached this backwards. I just wanted to show one pos= sible use case for supporting local vs. non-local class methods. Your in-me= thod conditional solution works for this specific case, true. But how well = does it translate to other cases? For instance, it would not work with anon= ymous classes. called_by_super is an interesting notion, but after some tho= ught it feels like a make shift approach that only address part of the wide= r issue. The module non-local side of this is important too. And its use ca= se is much more obvious --everywhere the included/ClassMethods hack is used= .




----------------------------------------
Feature #7914: Case for local class methods
https://bugs.ruby-lang.org/issues/7914#change-36827

Author: trans (Thomas Sawyer)
Status: Open
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version: next minor


=3Dbegin
Here is a use case for local class methods.

Say we wish to give certain classes and all subclasses a special name.

=9A class X
=9A =9A def self.special_name
=9A =9A =9A "special:#{name}"
=9A =9A end
=9A end
=9A class Y < X; end
=9A class Z < Y; end

=9A Z.special_name =9A#=3D> "special:Z"

But what if Y has a unique special name?

=9A class Y < X
=9A =9A def special_name
=9A =9A =9A 'unique:Y'
=9A =9A end
=9A end

Problem that arises:

=9A =9A Z.special_name =9A#=3D> "unique:Y" =9A# wrong!

Currently, to solve this would require creating an additional method, e.g. = `unique_name` and redefine `special_name` to first look for unique_name the= n fallback to default special name if non-found. It works, but adds additio= nal complexity to API.

Nicer solution would be local class methods.

=9A =9A class Y < X
=9A =9A =9A def special_name
=9A =9A =9A =9A 'unique:Y'
=9A =9A =9A end
=9A =9A =9A local :special_name
=9A =9A end

=9A =9A Y.special_name =9A#=3D> "unique:Y"
=9A =9A Z.special_name =9A#=3D> "special:Z"

The idea being that local class methods are skipped in super/lookup chain.<= br>
This idea is not without precedence. Module class methods can be thought of= as being local. So this idea has other side of the notion, that modules co= uld have class methods that are not skipped over in the super/lookup chain.= In that case we would need a term that means opposite of local, so I'l= l use `nonlocal`:

=9A =9A module M
=9A =9A =9A def self.q; "q"; end
=9A =9A =9A nonlocal :q
=9A =9A end

=9A =9A class X
=9A =9A =9A include M
=9A =9A end

=9A =9A X.q =9A#=3D> "q"

=3Dend



--
http://bugs.ruby-l= ang.org/

--f46d044306aa1880dc04d6676835--