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

Re: Blessed objects are `isa` their reftype

Thread Previous | Thread Next
From:
=?UTF-8?Q?Salvador_Fandi=c3=b1o?=
Date:
February 19, 2021 09:06
Subject:
Re: Blessed objects are `isa` their reftype
Message ID:
7f4c2812-21df-aa1d-7bc8-d73386268beb@gmail.com
On 19/2/21 0:33, Paul "LeoNerd" Evans wrote:
 > On Fri, 19 Feb 2021 08:12:30 +0900
 > Yuki Kimoto <kimoto.yuki@gmail.com> 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:

https://grep.metacpan.org/search?q=UNIVERSAL%3A%3Aisa%5C%28%5B%5E%5C%29%5D*%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 
keep-it-consistent-with-the-old.

Cheers

Thread Previous | 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