1Syntax::Keyword::MultiSUusbe(r3)Contributed Perl DocumenStyanttiaoxn::Keyword::MultiSub(3)
2
3
4

NAME

6       "Syntax::Keyword::MultiSub" - multiple dispatch on subroutines
7

SYNOPSIS

9          use v5.26;
10          use Syntax::Keyword::MultiSub;
11          use experimental 'signatures';
12
13          multi sub max()          { return undef; }
14          multi sub max($x)        { return $x; }
15          multi sub max($x, @more) { my $y = max(@more);
16                                     return $x > $y ? $x : $y; }
17
18          say max(1, 2, 15, 3, 4);  # prints 15
19

DESCRIPTION

21       This module provides a new keyword, "multi", to put before subroutine
22       declarations, which permits multiple distinct function bodies to be
23       provided, which take different parameters. A call to a "multi sub" will
24       invoke whichever function body best fits the arguments passed.
25
26       Currently this module can only make dispatching decisions based on the
27       number of arguments as compared to the number of signature parameters
28       each body was expecting. It requires perl version 5.26 or above, in
29       order to get enough support from signatures. Note also enabling this
30       module does not enable the "signatures" feature; you must do that
31       independently.
32

KEYWORDS

34   multi
35          multi sub NAME (SIGNATURE) { BODY... }
36
37       Declares an alternative for the "multi sub" of the given name. Each
38       alternative will be distinguished by the number of parameters its
39       signature declares. If the signature includes optional parameters, this
40       alternative is considered to cover the entire range from none to all of
41       the optional ones being present. The ranges of parameter count covered
42       by every alternative to a given function name must be non-overlapping;
43       it is a compiletime error for two function bodies to claim the same
44       number of parameters.
45
46       Each of the non-final alternatives for any given name must use only
47       scalar parameters (though some may be optional); but as a special-case,
48       the final alternative may end in a slurpy parameter (either an array or
49       a hash). If this is the case then it will be considered for dispatch if
50       none of the previous alternatives match, as long as it has at least the
51       minimum number of required parameters present.
52

WITH OTHER MODULES

54   Future::AsyncAwait
55       As of Future::AsyncAwait version 0.55 a cross-module integration test
56       asserts that the "multi" modifier can be applied to "async sub".
57
58          use Future::AsyncAwait;
59          use Syntax::Keyword::MultiSub;
60
61          async multi sub f () { return "nothing"; }
62          async multi sub f ($key) { return await get_thing($key); }
63

TODO

65       •   Much better error checking and diagnostics for function bodies that
66           don't use signatures.
67
68       •   Cross-module testing with Object::Pad (for "multi method"). This
69           may require a better combined implementation, to be aware of method
70           resolution order, inheritence, etc...
71
72       •   An eventual consideration of type assertions or value testing, as
73           well as simple argument count.
74
75           This particular task is likely to be a large undertaking as it
76           spans several other areas of language. As well as types on
77           parameters, it would be nice to put them on lexical variables,
78           object slots, "match/case" comparisons, and so on. It would be a
79           shame to invent a special mechanism for one of these areas that
80           could not be reüsed by the others.
81

AUTHOR

83       Paul Evans <leonerd@leonerd.org.uk>
84
85
86
87perl v5.36.1                      2023-07-13      Syntax::Keyword::MultiSub(3)
Impressum