develooper Front page | perl.perl6.users | Postings from June 2018

Re: mixin syntax: does vs but

Thread Previous | Thread Next
From:
Joseph Brenner
Date:
June 12, 2018 17:36
Subject:
Re: mixin syntax: does vs but
Message ID:
CAFfgvXVA9xuV6UC3zmsvpvQPKRaVEA+Ud0aQP8dpeieGCAEq8Q@mail.gmail.com
> Use binding:
>
> my @x= <1 2 3>; my @y := @x but Iterable; say @y.^name; #  OUTPUT: «Array+{Iterable}␤»

Hm... the docs on objects has this example:

https://docs.perl6.org/language/objects

  role R { method Str() {'hidden!'} };
  my $i = 2 but R;
  sub f(\bound){ put bound };
  f($i); # OUTPUT: «hidden!␤»

So, my mistake was thinking the form of that second line would work
with array variables?

Looking up "binding" in the docs, there's this material:

https://docs.perl6.org/language/list

  By default, when you assign a List to an @-sigiled variable, you
create an Array. Those are described below. If instead you want to
refer directly to a List object using an @-sigiled variable, you can
use binding with := instead.

     my @a := 1, 2, 3;

  One of the ways @-sigiled variables act like lists is by always
supporting positional subscripting. Anything bound to a @-sigiled
value must support the Positional role which guarantees that this is
going to fail:

     my @a := 1; # Type check failed in binding; expected Positional but got Int

And my first reaction to all this is "Seriously?!".  There are
multiple different things here that feel like syntactic glitches, and
you need to tell me about the Positional role now to have any hope of
convincing me this one makes sense on some level?

(One reason you don't want me on stackoverflow is that I'm a whiner
who is not likely to whole-heartedly promote the cause...)



On Tue, Jun 12, 2018 at 12:09 AM, JJ Merelo <jjmerelo@gmail.com> wrote:
> Use binding:
>
> my @x= <1 2 3>; my @y := @x but Iterable; say @y.^name; #  OUTPUT:
> «Array+{Iterable}␤»
>
> El mar., 12 jun. 2018 a las 9:06, Joseph Brenner (<doomvox@gmail.com>)
> escribió:
>>
>> I thought this would work to make a copy of @x but with the role
>> "LookInside" attached to it:
>>
>>    my @y = @x but LookInside;
>>
>> But that didn't add the role to @y. E.g.
>>
>>   say @y.^WHAT
>>
>> Would just report (Array), not (Array+{LookInside}).
>>
>> I found that this would do what I was trying to do though:
>>
>>    my @y = @x;
>>    @y does LookInside;
>>
>> I didn't think there would be any difference between the two
>> though.  What am I not getting?
>>
>> The full code looks like this:
>>
>> trial_introspect.pl6:
>>
>>   use v6;
>>   use Trial::Introspect;
>>   my @x = <wuhn tew thuree foah fahv sex>;
>>   my @y = @x;
>>   @y does LookInside;
>>   say "Methods: ";
>>   say @y.methodical_methods;
>>
>>
>> .../Trial/Introspect.pm6:
>>
>>   role LookInside {
>>     method methodical_methods {
>>       self.^methods.map({ .gist }).sort.unique.map({ "$_\n" }).grep({
>> ! /^Method/ });
>>     }
>>   }
>
>
>
> --
> JJ

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