On 16/09/17 05:19, ToddAndMargo wrote:
> On 09/13/2017 01:36 PM, Trey Harris wrote:
>> L1 is an “ordinary method” (denoted, obviously enough, with the
>> declarator |method|), which as described in the Typesystem
>> <https://docs.perl6.org/language/typesystem#Methods> doc, “defines
>> objects of type Method <https://docs.perl6.org/type/Method> and binds
>> them to the provided name in the scope of a class.” An ordinary
>> method is usually called using dot syntax, i.e., |$object.getc(...)|.
>
> Okay.  Where did "$object" come from?
>
> It would help if it was written out in long hand so as to explain
> what the "." does.  Still have to have an example to figure out
> what is going on.

$object is just a stand-in for anything that would have the getc method
you're interested in. You can see from the declaration that it's defined
on IO::Handle. The . in $object.getc() means roughly this:

On the object in the variable $object find a method called "getc" and
call it, passing the $object itself as the "self" (also known as invocant).

>> Being a sub, it will be called most often using normal sub-calling
>> syntax, such as |getc($x)|.
>>
>
> Is $x called $object above?

Yes, that's right. Can be any variable that you put an IO::Handle into.

>
>> Now let’s turn to the /return values,/ which are what functional
>> programmers are usually looking at first. They’re marked by |-->|
>>
>
> --> means some kind of pointer to me.  "returns" would be much better
> wording for me, but at least
> I know what they are getting at.

Sadly, "returns" has limitations in certain situations, that's why the
--> syntax is generally preferred.

>> (though some old documentation may use |returns|). Conveniently, both
>> variants L1 and L2 return the same thing (not uncommon, for multi
>> routines): |Str:D|. The |Str| part is a reference to the string type
>> Str <https://docs.perl6.org/type/Str>. The |:D| suffix says that the
>> value is /defined/; in other words, |getc| will never return the
>> undefined value. (A |:U| suffix would indicate that the return is
>> always /undefined/;
>>
>
> I would love a reference to all that Str:D stuff that defined that
> kind of stuff.
>
> Do you mean "can be undefined"?  Always "undefined" would be useless.

No, 'Always "undefined"' is a useful concept in Perl 6, because type
objects are undefined values, and passing around type objects can be
interesting.
>
>> the default, a lack of any suffix, can be made explicit with |:_|,
>> and means—as in Perl 5—that the return might or might not be defined.)
>>
>> That gets you to the /arguments/ of the two variants. Each are
>> /unary;/ they take just one argument.
>>
>
> Okay, where?

I'm not sure how you mean this. But if you mean "where do you see that
they only take one argument", you can see it from the lack of commas in
the signature. There's just the IO::Handle part. In the method form it
has a "must be defined" constraint, and is pointed out to be a
restriction on the invocant - the thing before the .getc() in the
earlier example). In the sub form it's got an explicit name, $fh, and a
default value, $*ARGFILES. After the arguments there's also the return
type which is introduced with the --> syntax.

>> Putting it all together, it tells you that these are valid examples:
>> |my Str $chr = getc($*IN); # insist on STDIN, even if file arguments
>> were given on the command line $chr = "/dev/tty".IO.open.getc; #
>> Insist not just on STDIN, but on the POSIX tty |
>>
>> Learning to read the Perl 6 doc <https://docs.perl6.org/> signatures
>> may be frustrating at first, but it’s well worth it, and pays dividends.
>>
>> ​
> What in the world is "my Str $chr"?
>
> If returning a type String, why are you using "chr".  I though Perl
> had no defined character type.

"chr" here is only the name of the variable, it has no extra meaning to
the language. The type it's declared as remains Str. Calling it $chr
makes sense, as getc is supposed to only return one character at a time.


> I can see that, I would still need examples.  I was very puzzled about
> what you meant
> until your last two line, which made everything make sense.
>
> How you got from 
>         multi sub getc (IO::Handle $fh = $*ARGFILES --> Str:D)
> to
>        chr = "/dev/tty".IO.open.getc;
>
> is a complete mystery.  How I am suppose to know
> "(IO::Handle $fh = $*ARGFILES" means it wants a file handle?

Here you've switched the method and sub forms around. from the ".getc"
in the example you can tell it's a method call, not a sub call, as those
would look like "$chr = getc()" for example. But I'll explain the sub
form anyway:

That it takes a file handle is in the very first part of the argument
declaration. "IO::Handle $fh = $*ARGFILES" can be taken apart into four
pieces:

1) Whatever is passed must type-check as an IO::Handle
2) The variable inside the sub will be called "$fh" (the name doesn't
make a difference to the caller, but it's good for documentation purposes)
3) The variable passed must fit into a $ variable (because it's $fh
rather than &fh, @fh, or %fh)
4) If nothing gets passed, the $fh will use the value of $*ARGFILES instead.

> This is what I "think" you just said:
>
>    method getc(IO::Handle:D: --> Str:D)
>         1)  Requires an IO handle that can not be undefined
>         2)  Returns a string an is never undefined.
>         example:   my $Str = getc($*IN);   #   $*IN is STDIN

You put the "method" declaration up front and explain it properly, but
the example uses the subroutine form instead.

>    multi sub getc (IO::Handle $fh = $*ARGFILES --> Str:D)
>         1) "multi" means each sub's results are passed to the next sub
> in line
>         2) Requires you create a handle for it ("filename".IO.open)
>         3) Returns a string an is never undefined.
>         example:  my $Str = "/dev/tty".IO.open.getc;

Here you have the subroutine declaration up front and use the method
call form in the example.

>    and $*&# is just Yosemite Sam cussing.
>
> Thank you for trying to break through the fog
>
> -T  

I hope that clears things up a bit!
  - Timo

Reply via email to