Call a function: Difference between revisions

m
Fix Perl6 -> Raku in comments
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
m (Fix Perl6 -> Raku in comments)
Line 3,819:
(formerly Perl 6)
===Theory===
Fundamentally, nearly everything you do in Perl 6Raku is a function call if you look hard enough.
At the lowest level, a function call merely requires a reference to any
kind of invokable object, and a call to its <tt>postcircumfix:&lt;( )&gt;</tt> method.
Line 3,857:
to dispatch to the same set of functions that a function call of that name
would invoke. That's why there's a dispatcher, after all. Methods are declared
with a different keyword, <tt>method</tt>, in Perl 6Raku, but all that does is
install the actual function into a metaclass. Once it's there, it's merely
a function that expects its first argument to be the invocant object. Hence we
Line 3,865:
multiply dispatched to all lexically scoped candidates for the function. Hence
the candidate list is bound early, and the function itself can be bound early
if the type is known. Perl 6Raku maintains a clear distinction between early-bound
linguistic constructs that force Perlish semantics, and late-bound OO dispatch
that puts the objects and/or classes in charge of semantics. (In any case, <tt>&foo</tt>,
Line 3,908:
on whether a signature accepts a list at that position in the argument list, but
describing that is not the purpose of this task. Suffice to say that we assume here that the
foo function is declared with a signature of the form (*@params). The calls above might be interpreted as having a single array argument if the signature indicates a normal parameter instead of a variadic one. What you cannot do in Perl 6Raku (unlike Perl 5) is pass an array as several fixed arguments. By default it must either represent a single argument, or be part of a variadic list. You can force the extra level of argument list interpolation using a prefix <tt>|</tt> however:
 
<lang perl6>my @args = 1,2,3;
Line 3,936:
 
There is no difference between calling builtins and user-defined functions and operators (or
even control stuctures). This was a major design goal of Perl 6Raku, and apart from a very few
low-level primitives, all of Perl 6Raku can be written in Perl 6Raku.
 
There is no difference between calling subroutines and functions in Perl 6Raku, other than that
calling a function in void context that has no side effects is likely to get you a "Useless use of..." warning.
And, of course, the fact that pure functions can participate in more optimizations such as constant folding.
10,333

edits