> On Dec 20, 2020, at 2:54 AM, ToddAndMargo via perl6-users 
> <perl6-users@perl.org> wrote:

—snip--

> I obviously can't feed it a string.

Obviously you cannot, but actually you can!

$ raku -e 'say .cos for 42, "42", ("4" ~ "2"), "5421".substr(1,2);'
        -0.3999853149883513
        -0.3999853149883513
        -0.3999853149883513
        -0.3999853149883513

The subroutine form in https://docs.raku.org/routine/cos is declared as:
        sub cos(Numeric(Cool))
and the text specifies:
        Coerces the invocant (or in sub form, the argument) to Numeric, 
interprets it as radians, returns its cosine.
, so the string “42” is coerced to the number 42 as it is passed to cos().
This kind of coercion is not often needed in user code, but the code of the 
Raku core needs to coerce to maintain a key element of Perl&Raku design:
        * If the coder treats something as a number, then the language acts on 
it as if it is a number, even if it has to convert a string to a number behind 
the scenes at runtime.
        * If the coder treats something as a string, then the language acts on 
it as if it is a string, even if it has to convert a number to a string behind 
the scenes at runtime.
(BTW, both Perl and Raku use an amazingly efficient auto-caching of this 
string-vs-number duality; many newcomers think the conversion must be a 
performance hog, but it is not.)

For details on the syntax of automatic coercion during a subroutine call, see:
        https://docs.raku.org/type/Signature#Coercion_type
(Most users never need in their own code, but you saw it by reading the Raku 
core, so please take that as just for your curiosity and not as a new technique 
looking for a place in your own designs.) 

> On Dec 20, 2020, at 2:17 AM, ToddAndMargo via perl6-users 
> <perl6-users@perl.org> wrote:
> https://docs.raku.org/routine/cos
>    method cos()
> Where is the definition of what is fed to the method?
—snip—
The definition is hiding right in front of us :^)
Just like (in another thread) you defined `method BadAdd () {…}`, the `cos` 
method has open+close parens to show that it has empty Signature. It takes no 
arguments, and will complain if you try to pass an argument to it.
All methods automatically have access to the invoking object, via “self” (and 
other shortcuts like $.attribute_name).
When you call `42.cos`, `42` is the invoking object, and that one value is all 
that .cos needs.
        

> On Dec 20, 2020, at 2:25 AM, ToddAndMargo via perl6-users 
> <perl6-users@perl.org> wrote:

—snip--

> https://github.com/rakudo/rakudo/blob/master/src/core.c/Cool.pm6
> 
> 13: method cos()  { self.Numeric.cos }
> 
> Whatever the heck "self” is

“self” is the “current” object instance (called the “invocant”).
        https://docs.raku.org/language/terms#term_self
When you define a method in a Class, you are writing sort of abstractly, 
because at any given time you might have many objects (called “instances”) of 
that Class, all alive and working in your program at the same time.
In most languages that support OO, there is a magic keyword like “this” or 
“self” that refers to *only* that single instance that called the method.
In Raku, “self” is that keyword, but you can override it to a variable name of 
your choice , if that makes more sense in your code.
        my Str $a = ’15';
        my Str $b = '37';
        say $b.cos;
Inside method `cos`, during the .cos call in the code above, “self” is an alias 
to $b .


— 
Hope this helps,
Bruce Gray (Util of PerlMonks)

Reply via email to