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

Re: Blessed objects are `isa` their reftype

Thread Previous | Thread Next
From:
Paul "LeoNerd" Evans
Date:
February 16, 2021 22:19
Subject:
Re: Blessed objects are `isa` their reftype
Message ID:
20210216221909.0b63a42d@shy.leonerd.org.uk
On Tue, 16 Feb 2021 14:05:16 -0800
David Christensen <dpchrist@holgerdanske.com> wrote:

> On 2021-02-16 13:17, Dan Book wrote:
> > On Tue, Feb 16, 2021 at 4:14 PM David Christensen
> > <dpchrist@holgerdanske.com> wrote:
> >   
> >> On 2021-02-16 04:09, Paul "LeoNerd" Evans wrote:  
> >>> 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(!)  
> >>
> >>
> >> Please post a valid Perl program that demonstrates your point (e.g.
> >> Test::More, ok, TODO).
> >>  
> > 
> > The 'isa' feature must be enabled and requires Perl 5.32+.  
> 
> 
> 2021-02-16 14:02:27 dpchrist@f3 ~/sandbox/perl/p5p
> $ freebsd-version ; uname -a ; perl -v | head -n 2
> 12.1-RELEASE-p13
> FreeBSD f3.tracy.holgerdanske.com 12.1-RELEASE-p13 FreeBSD 
> 12.1-RELEASE-p13 GENERIC  amd64
> 
> This is perl 5, version 32, subversion 1 (v5.32.1) built for 
> amd64-freebsd-thread-multi
> 
> 2021-02-16 14:02:32 dpchrist@f3 ~/sandbox/perl/p5p
> $ nl paul-leonerd-evans-20210216-0409.pl
>       1	  my $aref = [ 1, 2, 3 ];
>       2	  $aref isa ARRAY             # is false
>        	
>       3	  my $aobj = bless [ 5, 6, 7 ], "MyArray::Class";
>       4	  $aobj isa MyArray::Class    # is true
>       5	  $aobj isa ARRAY             # is true(!)
> 
> 2021-02-16 14:02:42 dpchrist@f3 ~/sandbox/perl/p5p
> $ perl -Eisa paul-leonerd-evans-20210216-0409.pl
> 
> 
> Better, but I still do not see any output that demonstrates the OP's
> point.

  $ perl5.32.1
  use feature 'say';
  use experimental 'isa';

  my $aref = [ 1, 2, 3 ];
  say "1.TRUE" if $aref isa ARRAY;

  my $aobj = bless [ 5, 6, 7 ], "MyArray::Class";
  say "2.TRUE" if $aobj isa MyArray::Class;
  say "3.TRUE" if $aobj isa ARRAY;
  ^D
  2.TRUE
  3.TRUE

The one numbered 3 is surprising.

-- 
Paul "LeoNerd" Evans

leonerd@leonerd.org.uk      |  https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/  |  https://www.tindie.com/stores/leonerd/

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