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

Re: Blessed objects are `isa` their reftype

Thread Previous | Thread Next
February 19, 2021 09:06
Re: Blessed objects are `isa` their reftype
Message ID:
On 19/2/21 0:33, Paul "LeoNerd" Evans wrote:
 > On Fri, 19 Feb 2021 08:12:30 +0900
 > Yuki Kimoto <> wrote:
 >> Sorry, I misunderstanded.
 >> perl -e 'print( (bless [], "Foo")->isa("ARRAY")) . "\n"'
 >> result is 1.
 >> This is originally isa behavior.
 > Correct; that's how the ->isa method works.
 > I just don't think that's useful.

Well, if you mean just that way of dispatching "isa" as a object method, 
I agree with you that it is not very useful because it doesn't work on 
bare arrays (for instance '[]->isa("ARRAY")').

On the other hand the construction 'UNIVERSAL::isa($whatever, "ARRAY")' 
is pretty common:*%2C%5Cs*%5B%22%27%5DARRAY&qd=&qft=

I think that the underlying problem here, is that "isa" is being used 
for two completely different things.

1) checking the class hierarchy.

2) checking the base data structure used to build some object.

Maybe the solution is to add a new operator, for instance, "based_on", 
so that one can write code as:

    $foo = bless [], Foo;
    if ($foo isa "ARRAY") {...} # ==> FALSE
    if ($foo based_on "ARRAY") {...} # ==> TRUE

Maybe an interesting thing of having this operator is that it could be 
overloaded, so that objects overloading dereferencing operators could 
mimic better the real base data structures. That has been traditionally 
a pain point with 'isa'.

Otherwise, another solution would be to just educate people to use 
"reftype" for that as in...

     if (reftype($foo) eq "ARRAY") {...}

instead of...

     if ($foo isa "ARRAY") {...}

I think my opinion regarding the "isa" operator is more and more 
drifting to the do-the-right-thing camp instead of the former 


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