Re: Featurewish: Context aware operator overloading

[email protected] (Abigail)
Newsgroups perl.perl5.porters
Message-ID <20100616165804.GH3367@almanda>
On Tue, May 25, 2010 at 04:44:32PM +0200, Rafael Garcia-Suarez wrote:
> On 25 May 2010 15:36, Steffen Schwigon <[email protected]> wrote:
> > Hi!
> >
> > Forgive me this passive way of just wishing a feature. I can't provide
> > more than a user's perspective. Anyway:
> >
> > Would it make sense to make operator overloading context aware?
> >
> > Currently an overloaded operator only runs in scalar
> > context. Therefore I do nasty dereferencing like
> >
> >  my @list = @{ $data ~~ dpath '//affe/zomtec' };
> >
> > just because the overloaded '~~' cannot return an array.
> >
> > What I would want is
> >
> >  my @list = $data ~~ dpath '//affe/zomtec';
> >  my $head = $data ~~ dpath '//affe/zomtec';
> >
> > with the sub behind the overloaded '~~' doing the usual wantarray
> > drill.
> >
> > I have, however, no more knowledge about this, except that someone
> > once said the original author of overloading hadn't found it
> > neccessary, IIRC.
> >
> > And yes, I know that this would probably introduce trouble on
> > backwards compat. Maybe it can be protected by some “use feature”
> > magic.
> 
> A feature pragma wouldn't be needed if we pass the context as a new
> parameter to the overloading method, with the default (undef)
> indicating scalar context.


That would break code, wouldn't it? 

If

    my @foo = X ~~ Y

now calls the overload sub in scalar context, changing it to run it in
list context will cause code breakage, regardless how you pass the
information around.

Or am I misunderstanding things?


Not that I would care too much about this breakage - I'd call it a bug fix ;-)


Abigail
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.