develooper Front page | perl.perl5.porters | Postings from February 2021

Blessed objects are `isa` their reftype

Thread Next
Paul "LeoNerd" Evans
February 16, 2021 12:09
Blessed objects are `isa` their reftype
Message ID:
There is an odd asymmetry with isa when testing if an object is derived
from its reference type:

  my $aref = [ 1, 2, 3 ];
  $aref isa ARRAY             # is false

  my $aobj = bless [ 5, 6, 7 ], "MyArray::Class";
  $aobj isa MyArray::Class    # is true
  $aobj isa ARRAY             # is true(!)

As further explained by documentation I have just added in my new

  This is done in order to preserve the equivalence to the
  C<UNIVERSAL::isa> method, which would also yield true in the same
  circumstance. Despite this, it is not true that C<$aobj> would
  consider methods in the C<ARRAY::> package as part of its method
  resolution order.

I'm not terribly comfortable with this situation, but annoyingly any
other solution feels worse:

 1. `isa` could warn (or croak?) on attempts to test the known all-caps
    reftype names (which are NOT perl packages) 

 2. `isa` could specifically omit these (and thus break equivalence
    with the `->isa` method)

Or something else.

If we accept that the current behaviour is the "least bad" of them and
we don't want to change it, then perhaps we can decide to
de-experimentalize this operator now it's been sitting unchanged for
all of 5.32, in time for a proper addition into 5.34.

Thoughts, anyone?

(cross-posted to github

Paul "LeoNerd" Evans      |  |

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