develooper Front page | perl.perl5.porters | Postings from June 2008

smartmatch and overloaded objects in blead

Thread Next
From:
Yuval Kogman
Date:
June 27, 2008 00:01
Subject:
smartmatch and overloaded objects in blead
Message ID:
20080627070126.GS23537@woobling.org
I can't find the thread where this was discussed.

I think the but the new mandatory overloading for smartmatching of
objects is too restrictive.

Here are a few cases that I think could be changed:


./perl -Ilib -E 'my $obj = bless {}, "Foo"; $obj ~~ sub { 1 }'
Smart matching a non-overloaded object breaks encapsulation at -e
line 1.

The subroutine in question does not break encapsulation in this
particular instance and there are many valid uses.




./perl -Ilib -E 'my $obj = bless {}, "Foo"; $obj ~~ $obj'
Smart matching a non-overloaded object breaks encapsulation at -e
line 1.

This case could be refaddr equality, comparing an object to itself
doesn't sound like encapsulation breaking to me.

Likewise all comparisons of non overloaded objects which aren't the
same could just return false.




./perl -Ilib -E 'my $obj = bless {}, "Foo"; $obj ~~ "Foo"'
Smart matching a non-overloaded object breaks encapsulation at -e
line 1.

This could be repurposed to call ->isa, it's *VERY* useful for
checking exceptions.

The problem is that the string would of course return true for
$obj = "Foo" or $obj = ["Foo"], so this case is ambiguous, and maybe
not such a good idea.





./perl -Ilib -E 'my $obj = bless {}, "Foo"; warn $obj ~~ undef'
Smart matching a non-overloaded object breaks encapsulation at -e
line 1.

perlsyn says:
        Any     undef     undefined                !defined $a

!defined($obj) does not break encapsulation IMHO




And while we're on the subject:

./perl -Ilib -E 'warn sub { warn "First" } ~~ sub { warn "second" }'
Warning: something's wrong at -e line 1.

./perl -Ilib -E 'warn sub () { warn "First" } ~~ sub () { warn
"second" }'
Warning: something's wrong at -e line 1.


This seems to contradict the documentation that implies that
subroutines with a prototype of "" are treated more like values
(which fwiw I think is wrong, I prefer the current behavior that
compares refs, if I wanted to check the value i'd deref the sub)

At any rate

   Code()  Code()    results are equal        $a−>() eq $b−>()

doesn't seem to apply to anything.

I'd argue that anyway it ought to be $a->() ~~ $b->() but I don't
see that it's ever used.


-- 
  Yuval Kogman <nothingmuch@woobling.org>
http://nothingmuch.woobling.org  0xEBD27418


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About