On 10/26/05, Rob Kinyon <[EMAIL PROTECTED]> wrote:
> On 10/26/05, chromatic <[EMAIL PROTECTED]> wrote:
> > On Wed, 2005-10-26 at 20:29 -0400, Rob Kinyon wrote:
> >
> > > I would prefer to use roles as they're closed by default, leaving
> > > "class" to be my powertool, if I need the power.
> >
> > I don't understand this desire; can you explain your reasoning?
> If a role is an immutable class, that means that its internals cannot
> be changed. Hence, the compiler can trust that it will be the same at
> the end as at the beginning. Which means it's optimized. Which means
> my objects run faster if I create them from roles than if I create
> them from classes. And, given that this seems to be the sole
> difference between them (mutability vs. immutability), why would I use
> classes as my standard?

Okay, an open class means you can add methods to it, right?  So, let's
say you have this class:

    class Foo {
        method foo() {...}
        method bar() {...}

And this code:

    my Foo $x = Foo.new;

This might be compiled to the following pseudo intermediate code:

    my $x = Foo[0]();

Now let's say you extend the class:

   class Foo is also {
       method baz() {...}

Is there any reason that the preceding pseudo intermediate code is no
longer valid?  I don't see one; baz() is just installed in slot 3.  So
why would you say it was faster to have it closed?

Indeed, it *could* be faster.  But we find that many programmers make
decisions that trade readability and extensibility for an extra 1% of
speed, even when they are writing a command-line frontend to
MPlayer[1].  If those people are module writers, then we have a bunch
of modules on CPAN that are not friendly to the user who wants to use
the module in the one way the writer didn't expect.

So the reason we made classes open by default is so that people
wouldn't have a chance to make that decision until they're more
experienced.  Indeed, no module writer can say that a class is closed;
only the main program may say that, because the main program knows the
most about how everything is used.  The precise definition of "main
program" is not well-defined yet, but it's there so that a module
writer doesn't close himself off from the world without knowing how
his module is even being used.

And if you're going to use roles for everything because they're closed
and they will gain you 2% of speed on one particular backend, then
we'll have to make the same rule for them too.  I know it sounds like
we're "babying" our programmers.  We are, because it's such a
widespread superstition.

And just to reinforce that it's a superstition:  a theory defines a
vtable.  If you extend the class in an incompatible way, you have to
make a new instance of its theory, defining new vtable slots.  Once
the new vtable is created, it is just as fast as the old one.  There
is no speed loss whatsoever for keeping your class open.


[1] This is a concrete example that I actually witnessed.

Reply via email to