Dios

 view release on metacpan or  search on metacpan

lib/Dios.pm  view on Meta::CPAN

            callsame;         # Same as: goto &recursively_process_list;
        }
    }

Note that there is currently a syntactic restriction on C<callwith> (but
not on C<callsame>). Specifically, C<callwith> cannot be invoked with a
postfix qualifier. That is, none of these are allowed:

    callwith @args      if @args;
    callwith @args      unless $done;
    callwith get_next() while active();
    callwith get_next() until finished();
    callwith $_         for readline();

When you need to invoke C<callwidth> conditionally, use the block forms
of the various control structures:

    if (@args)         { callwith @args      }
    unless ($done)     { callwith @args      }
    while (active())   { callwith get_next() }
    until (finished()) { callwith get_next() }
    for (readline())   { callwith $_;        }


=head2 Declaring multifuncs and multimethods

Dios supports multiply dispatched functions and methods, which
can be declared using the C<multi> keyword.

Multiple dispatch is where two or more I<variants> of a given
function or method are defined, all of which have the same name,
but each of which has a unique parameter signature. When such
a function or method is called, Dios examines the arguments it
was passed and determines the most appropriate variant to invoke.

The rules for selecting the most appropriate variant as the same
as in Perl 6, namely:

=over

=item 1.

Eliminate every variant number or types of parameters does
not match the number and types of the argument list.

=item 2.

Sort the remaining viable variants according to how constrained their
parameter lists are. If two variants have equally constrained parameter
lists (or parameter lists for which there is no clear ordering of
constrainedness), sort them in order of declaration.

=item 3.

Call the first variant in the sorted list, or throw an exception if
the list is empty.

=back

Multifuncs and multimethods are a useful alternative to internal C<if>/C<elsif>
cascades. For example, instead of:

    func show (Num|Str|Array $x) {
        ref($x) eq 'ARRAY'     ?  '['.join(',', map {dump $_} @$a).']'
      : looks_like_number($x)  ?    $x
      :                           "'$x'"
    }

you could write:

    multi func show (Array $a) { '['.join(',', map {dump $_} @$a).']'}
    multi func show (Num   $n) {   $n   }
    multi func show (Str   $s) { "'$s'" }


Note that, when declaring a multifunc the C<func> keyword may be omitted
(as in Perl 6). So:

    multi func show (Num $n) {...}
    multi func show (Str $s) {...}
    multi func show (Ref $r) {...}

may also be written as just:

    multi show (Num $n) {...}
    multi show (Str $s) {...}
    multi show (Ref $r) {...}

Methods can also be declared C<multi>, in which case the class
of the invocant object is also considered when determining the
most appropriate variant to call. Multimethods are, of course,
inherited, and may be overridden in derived classes.


=head2 Declaring submethods

A I<submethod> is a Perl 6 construct: a method that is
not inherited, and hence may be called only on objects
of the actual class in which it is defined.

Dios provides a C<submethod> keyword to declare such methods.
For example:

    class Account {
        method trace_to (IO $fh) {
            carp "Can't trace a ", ref($self), " object";
        }
    }

    class Account::Traceable is Account {
        submethod trace_to (IO $fh) {
            print {$fh} $self->dump();
        }
    }

Now any objects in a class in the C<Account> hierarchy will complain if
its C<trace_to()> method is called, except objects in class
C<Account::Traceable>, where the submethod will be called instead of the
inherited method.

Most unusually, if the same method is called on an object of any class



( run in 1.647 second using v1.01-cache-2.11-cpan-63c85eba8c4 )