Is better to subclass or to add methods to an existing class?

“ts” wrote

This runs squarely against a prominent argument of the
anti multi-method camp'' that Ruby's open classes’’
(a very descriptive term btw.) somehow reduce the need
of method overloading.

Well expect that you forget that

Maybe this is also a good time to mention that the ``problems’’
you foresaw when playing around with your own overloading
implementation was imo caused by the fact that you allowed
modules (such as Enumerable) as (multi-)method signature types.

Had you only allowed genuine classes as signature types, you
probably would not have run into any major (non-performance)
problem.

/Christoph

What’s wrong with:

Beast = self
(@temptation = Class.new String).class_eval do
Beast = self
def six_six_six; Beast end
end

Paul

···

On Fri, Sep 20, 2002 at 05:41:58AM +0900, Christoph wrote:

Beast = self
(@temptation = Class.new String).class_eval <<-Body
Beast = self
def six_six_six; Beast end
Body

Well you would (better finally could) get ride of
coercion ...

In this case, just change the name of the language :slight_smile:

Guy Decoux

Maybe this is also a good time to mention that the ``problems''
you foresaw when playing around with your own overloading
implementation was imo caused by the fact that you allowed
modules (such as Enumerable) as (multi-)method signature types.

No, no. I've just added modules to kill it :slight_smile:

Another example, what do you expect with ?

   ruby -e 'class A < Array; end; a = A.new; b = A.new; p ((a+b).type)'

Guy Decoux

“Paul Brannan” pbrannan@atdesk.com wrote in message news:20020919172550.F6781@atdesk.com

Beast = self
(@temptation = Class.new String).class_eval <<-Body
Beast = self
def six_six_six; Beast end
Body

What’s wrong with:

Beast = self
(@temptation = Class.new String).class_eval do
Beast = self
def six_six_six; Beast end
end

(In 1.7 one can also use Class.new super_class { … } )

``proc type’’ class_eval’s do not change the scope. In other
words there is only one (the top) Beast around - hence

p six_six_six # =>
#<Object:0x1011b550 @temptation=#Class:0x1010a7f8>

A ``string type’’ class_eval creates the expected two
Beasts and six_sx_six picks up the inner Beast - hence

p six_six_six # =>
#Class:0x1010c388

/Christoph

···

On Fri, Sep 20, 2002 at 05:41:58AM +0900, Christoph wrote:

“ts” wrote in

Well you would (better finally could) get ride of
coercion …

In this case, just change the name of the language :slight_smile:

I grant you that overloading might change the language
for good or maybe bad? However getting ride of
coercion is no lose - coercion is complicated, slow,
and it is almost trivial to make mistakes …

/Christoph

“ts” wrote

No, no. I’ve just added modules to kill it :slight_smile:

Another example, what do you expect with ?

ruby -e ‘class A < Array; end; a = A.new; b = A.new; p ((a+b).type)’

I would bet on an A object. This behavior is somewhat problematic
(see the old “Subrange of String subclass” thread) but I don’t see
this as a problem of overloading.

Anyway I don’t think that ``general method overloading’', with
the possible exception of operators, is a good idea anyway.

Rather, the request of an ``overloaded method call’’ should be
made explicit - as in

<(a,b)>.foo( _not, *overloaded)

or

a.foo( b; also, _not, overloaded)

/Christoph

``proc type’’ class_eval’s do not change the scope. In other
words there is only one (the top) Beast around - hence

This must have changed from 1.6 to 1.7.

/ test.rb
Beast = self
(@temptation = Class.new String).class_eval <<-Body
Beast = self
def six_six_six; Beast end
Body

t = @temptation.new("foo")
p t.six_six_six
p Beast

Beast = self
(@temptation = Class.new String).class_eval do
  Beast = self
  def six_six_six; Beast end
end

t = @temptation.new("foo")
p t.six_six_six
p Beast

\ end test.rb

[pbrannan@zaphod tmp]$ ruby -v test.rb
ruby 1.6.7 (2002-03-01) [i686-linux]
#<Class 0lx4028b8b8>
#<Object:0x40296ce0 @temptation=#<Class 0lx4028b8b8>>
test.rb:11: warning: already initialized constant Beast
#<Class 0lx402892fc>
#<Object:0x40296ce0 @temptation=#<Class 0lx402892fc>>

[pbrannan@zaphod tmp]$ ruby-1.7 -v test.rb
ruby 1.7.3 (2002-09-04) [i686-linux]
#Class:0x401d2154
#<Object:0x401e0a74 @temptation=#Class:0x401d2154>
test.rb:11: warning: already initialized constant Beast
test.rb:13: warning: already initialized constant Beast
Beast
Beast

A ``string type’’ class_eval creates the expected two
Beasts and six_sx_six picks up the inner Beast - hence

I guess the string-type eval will work on both 1.6 and 1.7. I wonder if
there’s a way to get the same functionality on both 1.6 and 1.7 without
evaling a string.

Paul

···

On Fri, Sep 20, 2002 at 07:38:40AM +0900, Christoph wrote:

ruby -e 'class A < Array; end; a = A.new; b = A.new; p ((a+b).type)'

I would bet on an A object. This behavior is somewhat problematic
(see the old "Subrange of String subclass" thread) but I don't see
this as a problem of overloading.

If you add overloading, you add implicitely a new rule : to resolve a call
ruby compare 2 classes and select the *best* choice.

This is not the choice made by ruby in my example because its result is
Array

In short :
   * you have removed coercion
   * you change one rule of ruby

It's time, for you, to find a name for this new language (try to avoid P
for the first letter :-))

Guy Decoux

“ts” wrote

I would bet on an A object. This behavior is somewhat problematic
(see the old “Subrange of String subclass” thread) but I don’t see
this as a problem of overloading.

If you add overloading, you add implicitely a new rule : to resolve a call
ruby compare 2 classes and select the best choice.

I was not writing about my own preferences, just expectations,
(I strongly prefer ``Array’’ btw.) based on the imo wrong headed
type mutating behavior (no Ruby in side at the moment)

p A.new.sort, A.flatten, … # A, A, …

This is not the choice made by ruby in my example because its result is
Array

In short :

  • you have removed coercion

not a big loss;-)

  • you change one rule of ruby

I guess, adding overloading would be a much bigger
change …

It’s time, for you, to find a name for this new language (try to avoid P
for the first letter :-))

How, about Muby;-)

/Christoph