Re: RFC 152 (v1) Replace $self in @_ with self() builtin (not $ME)

[email protected] (Damian Conway)
Newsgroups perl.perl6.language.objects
Message-ID <[email protected]>
   > >Currently, the current object context is passed into a sub as the first
   > >element of @_, leading to the familiar construct:
   > 
   > >   my $self = shift;
   > 
   > >However, this is a big PITA. In particular, if you support lots of
   > >different calling forms (like CGI.pm), you have to check whether $_[0]
   > >is a ref, etc, etc, etc.
   > 
   > It is?  I don't see that this is a pain at all.  It seems like
   > a beautiful point of homogenization.  You don't force the user
   > to say $self; they could use $this if they wanted.  Heck, they 
   > don't need it at all.
   > 
   >     my(undef, @args) = @_;
   > 
   > Or as in 
   > 
   >     shift->fn(@_)
   > 
   > This is one of the things that Larry, in borrowing much of Perl's
   > OO from Python, carefully preserved from the parent tongue.  Don't
   > see why you would break it.


My forthcoming proposal will be that invocants still be passed as $_[0]
by default, but that there be a pragma allowing $_[0] to be automagically
shifted somewhere else during dispatch. For example:


	sub method { print "I was called through: $_[0]";
		     print "My args were: @_[1..$#_]";	}	#default


	use invocant '$ME';
	sub method { print "I was called through: $ME";
		     print "My args were: @_";		}


	use invocant '$MYSELF';
	sub method { print "I was called through: $MYSELF";
		     print "My args were: @_";		}


	use invocant 'self';
	sub method { print "I was called through: ", self;
		     print "My args were: @_";		}


	use invocant 'this';
	sub method { print "I was called through: ", this;
		     print "My args were: @_";		}


	use invocant 'the_great_and_powerful_OZ';
	sub method { print "I was called through: ", the_great_and_powerful_OZ;
		     print "My args were: @_";		}


Backwrds compatibility is preserved, everyone's religion is covered, and
the choice is made explicit.

Damian
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.