develooper Front page | perl.perl5.porters | Postings from July 2013

Re: postfix dereference syntax

Thread Previous | Thread Next
From:
Brad Gilbert
Date:
July 4, 2013 02:54
Subject:
Re: postfix dereference syntax
Message ID:
CAD2L-T3Uv=vs8gq2PsOBCmcqje+dPfAYodFcALJO3zB90HcgPQ@mail.gmail.com
On Wed, Jul 3, 2013 at 5:17 PM, David Golden <xdg@xdg.me> wrote:
> On Wed, Jul 3, 2013 at 5:51 PM, D Perrett <perrettdl@googlemail.com> wrote:
>>
>> object methods. Would we be better with a syntax for builtin methods?
>>
>>   $arrayRef->>flatten;
>>   $hashRef->>flatten;
>
> Rik said "no autoboxing" anyway, but just to complete the thought, I
> think that "methods on references" and "methods on objects" could
> coexist. If a programmer doesn't know whether they have a reference or
> an object, it's up to them to sort it out.  Objects should be opaque.
>
> Rather than ->> I'd prefer to just see it done with CORE, if that
> could be made to work, so it's similar to ordinary superclass method
> calling.
>
>    $maybe_object->CORE::flatten;
>
> If we had core classes for containers and if references were objects
> this would all be a lot easier, but that ship has probably sailed.

Actually how about ->^

    my %h = ( map{ $_, uc $_ } 'a'..'z' );
    my $h = \%h;
    my @a = ( 'a'..'z' );
    my $a = \@a;

    say for $h->^keys->^sort;
    say for $a->^keys;

    say for %h->^keys->^sort;
    say for %a->^keys;

    package Simple { sub new{bless {}} }

    my $obj = Simple->new;
    say for $obj->keys; # defers to ->^keys

    package Complex {
      our @ISA = 'Simple';
      sub keys{
        my ($self) = @_;
        sort keys %$self;
      }
    }

    $obj = Complex->new;
    say for $obj->keys; # sorted
    say for $obj->^keys; # unsorted

    $h->^keys->^sort->^say;

    push $a->^@, 1..9;
    $a->^push( 1..9 );

    my @letters = $a->map(sub{ /\A\w\z/ });

We could even add to ^sort to make it effectively
do a Schwartzian transform for us.

    @sorted = @unsorted->^sort(
      sub{ $a <=> $b }, # use numeric comparison
      sub{ length($_) } # but sort by the length of the string
    );

I would like to point out that P6 uses .^ to call methods on
an objects meta object.
Which means that these return the same thing in Perl6:

    say @a.^sort;
    say @a.HOW;
    say @a.HOW.sort;

So this may interfere with using ->^ for meta object
programming.
I think it may still be possible to use it for both
purposes though.

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