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

Re: mixin syntax: does vs but

Thread Previous | Thread Next
From:
Brandon Allbery
Date:
June 12, 2018 19:10
Subject:
Re: mixin syntax: does vs but
Message ID:
CAKFCL4WZJTia3Sw9fKxrbGvA7YKwetCzAsGVtd1WTWD_p3bhuQ@mail.gmail.com
I should clarify this, but I'm not recalling full details at the moment
which is why I didn't originally.

Perl uses a metaobject protocol (MOP, which you'll see in various places in
the docs). The "macro" to access the metaobject is the .HOW pseudo-method.
If you do this for a normal class or object of that class, you get
Perl6::Metamodel::ClassHOW back. This is what the .^method syntax is
accessing; it's short for (thing).HOW.method((thing), ...). The metaclass
doesn't magically know its children, so the object has to be used once to
get at its metaclass and a second time to tell the metaclass what it is to
introspect.

I'm not seeing documentation for what .WHAT actually does; it (correctly)
notes that it's implemented specially within the compiler (hence "macro")
but not how you achieve it otherwise. Then again, .HOW has the same issue;
there's a bit of a bootstrapping issue with getting at the metamodel, you
need to have it first. Which is why it's wired into the compiler and gets
those uppercase pseudo-method names.

On Tue, Jun 12, 2018 at 3:00 PM Brandon Allbery <allbery.b@gmail.com> wrote:

> .WHAT is a "macro"/shorthand, which is why it's uppercase. There's a
> metamodel (the real meaning of the ^) version of it as well.
>
> On Tue, Jun 12, 2018 at 2:59 PM Joseph Brenner <doomvox@gmail.com> wrote:
>
>> >> say @y.^WHAT
>>
>> > If you want to print the name use `.^name`.
>>
>> > If you want the type object for more advanced usages use `.WHAT`.
>>
>> Sorry, typo on my part.
>>
>> Though that raises another syntactic oddity I might whine about: perl6
>> code examples frequently use ".WHAT".  I was interested in getting a
>> list of all available methods, so I started trying some guesses:
>> ".METHODS", ".METHOD", ".methods".... But actually it's ".^methods".
>> Okay, the caret is used for introspection... but then why isn't it
>> ".^what"?
>>
>> (It also turns out that the list ".^methods" gives you is bizarre:
>> hard to read, has many duplicates, unsorted, full of odd entries that
>> look like internal use only widgets I don't care about just now...)
>>
>>
>>
>>
>> On Tue, Jun 12, 2018 at 11:34 AM, Brad Gilbert <b2gills@gmail.com> wrote:
>> > On Tue, Jun 12, 2018 at 12:55 PM, Joseph Brenner <doomvox@gmail.com>
>> wrote:
>> >> Thanks, both your suggestion and JJ Merelo's work, but I think I like
>> >> yours for readability:
>> >>
>> >>   # # using binding, suggested by JJ Merelo <jjmerelo@gmail.com>
>> >>   # my @y := @x but LookInside;
>> >>
>> >>   # suggested by Elizabeth Mattijsen liz@dijkmat.nl
>> >>   my @y does LookInside = @x;
>> >>
>> >> I actually found the use of "but" in the objects docs to be
>> >> tremendously confusing at first:  it looks like some sort of
>> >> conditional check, like "unless".
>> >
>> > The reason `but` exists is basically for the following
>> >
>> >     my $v = 0 but True;
>> >
>> >     if $v { say $v } # prints 0
>> >
>> > In Perl 5 it is common to return the string `"0 but true"` for a value
>> that is
>> > both 0 and true.
>> >
>> > Since one of the design philosophies of Perl 6 is to reduce the number
>> > of special cases this was made to be more generally useful.
>> >
>> > Note that you should not do the following
>> >
>> >     my $v = 0;
>> >     $v does True;
>> >
>> >     say 'WTF!' if 0; # prints WTF!
>> >
>> > Basically you can use `but` anywhere you like, but be careful with
>> `does`.
>> >
>> >> On Tue, Jun 12, 2018 at 1:01 AM, Elizabeth Mattijsen <liz@dijkmat.nl>
>> wrote:
>> >>>> On 12 Jun 2018, at 09:06, Joseph Brenner <doomvox@gmail.com> wrote:
>> >>>>
>> >>>> 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.
>> >
>> > That is effectively the same as:
>> >
>> >     my @y = (@x but LookInside).map: *.self;
>> >
>> > That is @ sigiled variables tend to slurp in iterable values.
>> >
>> >>>>  say @y.^WHAT
>> >
>> > If you want to print the name use `.^name`.
>> >
>> > If you want the type object for more advanced usages use `.WHAT`.
>> >
>> >>>>
>> >>>> Would just report (Array), not (Array+{LookInside}).
>> >>>
>> >>> What you’re doing here, is assigning the elements of an Array but
>> Lookinside to a normal Array.  The elements of the Array do not have any
>> roles mixed in, so you wind up with a normal Array with normal elements in
>> them.  Perhaps you meant:
>> >>>
>> >>>     my @y does LookInside = @x;
>> >>>
>> >>> Here you create an Array @y with the role LookInside mixed in, and
>> *then* assign the values from @x.
>> >>>
>>
>
>
> --
> brandon s allbery kf8nh                               sine nomine
> associates
> allbery.b@gmail.com
> ballbery@sinenomine.net
> unix, openafs, kerberos, infrastructure, xmonad
> http://sinenomine.net
>


-- 
brandon s allbery kf8nh                               sine nomine associates
allbery.b@gmail.com                                  ballbery@sinenomine.net
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net

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