develooper Front page | perl.perl6.language | Postings from July 2006

Re: Another quick one: .as<ancestor>

Thread Previous | Thread Next
Larry Wall
July 12, 2006 19:17
Re: Another quick one: .as<ancestor>
Message ID:
On Wed, Jul 12, 2006 at 12:51:57PM -0400, Aaron Sherman wrote:
: I would assume that all classes automatically define:
:  multi submethod *infix:<as> ($self: $?CLASS) { $self }

Hmm, "as" is really only intended for explicit type mutation (which
can work either by role mixin or by new object construction).  It's
not intended to give Perl a different "view" of an unmutated object.

: so that derived classes can automatically:
:  $<ancestor>

Nit: that's illegal syntax.  Or rather, it's legal, but you're saying


which is an attempt to do a hash subscript on whatever .as returns.
Only () and : cause arguments to be passed to .as.  So you have to
write one of

    $	# or .(Foo), \.(Foo), etc.
    $ Foo

(Note that those work because Foo is predeclared, presumably.)

: Without actually changing their implementation details (only the type
: that Perl currently thinks it's dealing with polymorphically).

Perl doesn't keep track of that in general except by marking a container
at compile time with the expected type.  But an object is always just
itself, unless you go through some kind of proxy or container.

: In fact, I would expect that this bit of behind-the-curtain magic is how
: the MCP arranges for polymorphism when you:
:  sub foo(Object $x) {...}
:  my A $y;
:  foo($y);

If you ask for the type of $x within foo(), it will tell you A, not Object.
(The type of variable($x) is Object, however.)

The behind-the-curtain polymorphic magic you're thinking of is really
handled within the various dispatchers, which call various methods
as if they were subroutines rather than methods.  That's how the
"lying" happens, generally.


Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About