Do I really not understand inheritance?

Or, to put it another way: it isn’t broken :slight_smile:

···

----- Original Message -----
From: dblack@candle.superlink.net

True, this isn’t a problem if you don’t allow Class to be subclassed;
Class.new is only broken if you remove the checks which prevent you from
noticing it’s broken… er, so to speak.


Exactly! Maybe it didn’t sound like it (my fault), but that is what I was
trying to say. “Broken” was meant to be very tongue-in-cheek. I mean, I
was essentially saying, “It’s broken, but there’s no way of knowing it.”
That’s my idea of a joke. :smiley: Sorry if that wasn’t well-conveyed; just my
peculiar sense of humor, I suppose.


But, if I’m correctly abstracting your underlying idea, I can imagine (at
least theoretically) that it might be good to be able to sort of take a
snapshot of an object’s behavior and capture it in/as a class.


In a sense, that’s already happening (if I understand you); it’s just not a
“class” class, it’s a singleton class.

I wanted to see if we could have the same functionality (the ability to
extend a single object) without the additional machinery of singleton
classes. I just thought that less machinery would allow for a more elegant
solution. I think I described a system pretty well in 58785 (which I don’t
think was archived because the archiving computer was down… it’s the one
with all of the diagrams). Assuming that you could subclass Class (and that
doing so worked properly), it would make sense and perhaps even allow all
sorts of new things we haven’t yet though of. (It seems like Ruby has
usually tried to avoid making unnecessary restrictions on what is allowed in
the language.)

At this point, I’d like to state that I’m not saying that such a change
would be worth the effort from cost/benefit statndpoint. But just for the
sake of argument:

If this system were implemented, what would we lose?

  • Extended objects would not be singletons of a singleton
    class. But why is that important? This would just be
    giving us a new option, as I see it: the ability to
    create a new object with the properties of the extended
    object.

  • In some cases, current occurrences of the #class method
    would have to be changed to the #kind_of? method. This
    doesn’t really seem like much of a problem, though.

  • The Singleton module wouldn’t work properly. This is
    (as I currently see it) the biggest argument against
    what I am suggesting. However, it would certainly be
    possible to implement this at the interpreter level,
    just not at the Ruby level. Being a singleton would
    probably be more a property of a class, rather than a
    mixin. It would also mean that you wouldn’t be allowed
    to extend a singleton object… but why would you ever
    need to? You could just extend its (singleton) class,
    instead.

  • Some other things which only matz could bring up. :slight_smile:

And what would we gain with such a system?

  • Little intuitive niceties, like having #kind_of? being
    simply an issue of looking in an object’s class’s
    ancestors, or having an object’s class describe all of
    the behavior of that object. This is where the seams
    in Ruby’s object/class model begin to show. The issue,
    as I see it, it that basically singleton classes are a
    form of multiple inheritance, which can’t help but
    cause problems. (Perhaps only small ones, but still.)

  • A simpler object/class model. Part of what is so great
    about Ruby is that clean design leads to wonderful,
    though often unforseen, results. If the design can be
    just a little cleaner still, it seems to me only a good
    thing. No more messing around with hidden singleton
    classes which matz considers abusive, anyway!

  • We can still extend objects using the exact same syntax.*
    In my eyes, extending an object and creating a singleton
    are really orthogonal concepts. We could still extend
    objects; we just wouldn’t be making them singletons at
    the same time.

  • More freedom as programmers. We’d be able to interact
    with the true class of an object (not some weird, hidden
    multiple inheritance thing). We’d be able to subclass
    Class. In general, I think we would get more unforseen
    goodness.

This is what I love so much about Ruby: a simple, clean language allows for
all sorts of unforseen goodness.

Chris

  • About that syntax… currently the “class << foo; …; end” construct
    creates a new singleton class if one didn’t already exist. If it did exist,
    however, the code simply extends the class. We could keep this behavior by
    allowing each object to create only one class in this way. (This is not
    the same as having a singleton class, which can only instantiate one
    object… it’s sort of the reverse.) However, if this new system were in
    place, perhaps we would find more natural ways to express object extension
    (without placing unusual, singleton-esque restrictions).

Hi –

I wanted to see if we could have the same functionality (the ability to
extend a single object) without the additional machinery of singleton
classes. I just thought that less machinery would allow for a more elegant
solution. I think I described a system pretty well in 58785 (which I don’t

Interesting – I perceive it as more machinery :slight_smile:

think was archived because the archiving computer was down… it’s the one
with all of the diagrams). Assuming that you could subclass Class (and that
doing so worked properly), it would make sense and perhaps even allow all
sorts of new things we haven’t yet though of.

Well… yes, but one could say the same thing about dividing by zero
:slight_smile:

I should probably reread some of this thread, because I can’t figure
out how subclassing Class got into the discussion.

And what would we gain with such a system?

  • Little intuitive niceties, like having #kind_of? being
    simply an issue of looking in an object’s class’s
    ancestors, or having an object’s class describe all of
    the behavior of that object. This is where the seams
    in Ruby’s object/class model begin to show. The issue,
    as I see it, it that basically singleton classes are a
    form of multiple inheritance, which can’t help but
    cause problems. (Perhaps only small ones, but still.)

I haven’t experienced any of these problems… but in any case, I
don’t think I’d connect this with multiple inheritance. In fact, I
think comparisons between Ruby’s design and other designs, while
sometimes helpful to bootstrap learning, usually end up being an
impediment. When people ask whether modules are the same as multiple
inheritance, or class methods are the same as static whatever methods
in Java (sorry, I’m Java illiterate), and so on, the answer is
usually, “You can get a foothold by looking at it that way, but not
really.” Singleton classes as multiple inheritance doesn’t ring true
to me.

  • A simpler object/class model. Part of what is so great
    about Ruby is that clean design leads to wonderful,
    though often unforseen, results. If the design can be
    just a little cleaner still, it seems to me only a good
    thing. No more messing around with hidden singleton
    classes which matz considers abusive, anyway!

I’ve never seen matz say that using them for their primary purpose
(creating singleton methods on objects) is abusive, only other
things. (I’m still waiting for a 10-word post from matz that will set
all of us straight :slight_smile:

  • We can still extend objects using the exact same syntax.*
    In my eyes, extending an object and creating a singleton
    are really orthogonal concepts. We could still extend
    objects; we just wouldn’t be making them singletons at
    the same time.

Modules are designed perfectly for exactly that.

  • More freedom as programmers. We’d be able to interact
    with the true class of an object (not some weird, hidden
    multiple inheritance thing). We’d be able to subclass
    Class. In general, I think we would get more unforseen
    goodness.

I’m wary of anything that promotes the idea of the “true class” of
objects in Ruby… though, as I said before, I’m intrigued by the
idea of being able to take a kind of snapshot of an object and
inferring a class object from it. But I don’t see any reason why
there would have to be a choice between that and the
per-object-oriented (so to speak) model of Ruby.

OK, enough banter :slight_smile: You should hack the interpreter to your liking
and show us some examples of things that are easier to write and/or
read. And I should see if I can create my class snapshot thingy.

David

···

On Fri, 13 Dec 2002, Chris Pine wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav