Is it too late for RFCs? How does one get approved for submission these
days? Or, do we just mail them off to [EMAIL PROTECTED]?

=head1 TITLE

Anonymous classes

=head1 VERSION

  Maintainer: Aaron Sherman <[EMAIL PROTECTED]>
  Date: 24 October 2001
  Version: 1
  Mailing List: perl6-language
  Number: 1

=head1 ABSTRACT

Anonymous classes are analogous to anonymous subroutines. They provide
an object with a unique namespace, lexical closure and method definitions.

=head1 DESCRIPTION

A while back on p5p, I suggested that:

        $obj->atexit(sub {...});

should be added to C<UNIVERSAL> (e.g. run this function when
I<this particular> instance goes out of scope).

Looking at Perl6, I see a better way that generalizes much more cleanly.

First off, let's look at anonymous classes:

        $x = class { sub .DESTROY { ... do cleanup ... } };

This creates a new object which exists in a class which is analogous
to an anonymous subroutine. This class is unique. It also provides
a closure in which its methods can access lexically scoped variables
within the caller's namespace.

So, C<.atexit> is:

        class UNIVERSAL;
        sub .atexit (&function) {
                my $tmp = $self; # To avoid clash inside nested method decl
                push $self.{_atexits}, class {
                        sub .DESTROY {
                                &function($tmp);
                        }
                };
        }

Of course, anonymous classes could be used for much more. Here's a silly
example that doesn't take advantage of the closure aspect at all (and
thus could have been done as a regular class):

        sub uc_uri ($uri) {
                return class {
                        sub .as_string { return uc $self.SUPER::as_string }
                } inherits('URI').new($uri);
        }

        my $URI = uc_uri("http://perl.com/";);

I know that this will be serious apple-cart-tilting material, but
I think the long-term advantages could be huge.

I can't see it all yet, but the idea of abstracting vector tables
back a step to include inheritance seems very appealing.

=head1 IMPLEMENTATION

The implementation would require an obvious change to the syntax of
the class declaration.

The syntax would be something like:

        class <name> BLOCK [inherits '(' LIST ')']

This would be a valid lvalue or rvalue.

Closures would have to be associatable with classes, and classes themselves
would have to be made to fit into lexical scope.

=head1 REFERENCES

=cut

-- 
Aaron Sherman
[EMAIL PROTECTED]             finger [EMAIL PROTECTED] for GPG info. Fingerprint:
www.ajs.com/~ajs        6DC1 F67A B9FB 2FBA D04C  619E FC35 5713 2676 CEAF
  "Write your letters in the sand for the day I'll take your hand
   In the land that our grandchildren knew." -Queen/_'39_

Reply via email to