declarator proto
1 | language documentation Functions |
1.1 | (Functions) declarator proto |
Documentation for declarator proto
assembled from the following types:
language documentation Functions
From Functions
(Functions) declarator proto
proto
is a way to formally declare commonalities between multi
candidates. It acts as a wrapper that can validate but not modify arguments. Consider this basic example:
proto congratulate(Str , Str , |)multi congratulate(, )multi congratulate(, , Int )congratulate('being a cool number', 'Fred'); # OKcongratulate('being a cool number', 'Fred', 42); # OK
congratulate('being a cool number', 42); # Proto match error
The proto insists that all multi congratulate
subs conform to the basic signature of two strings, optionally followed by further parameters. The |
is an un-named Capture
parameter, and allows a multi
to take additional arguments. The first two calls succeed, but the third fails (at compile time) because 42
doesn't match Str
.
say .signature # OUTPUT: «(Str $reason, Str $name, | is raw)»
You can give the proto
a function body, and place the {*}
where you want the dispatch to be done.
# attempts to notify someone -- False if unsuccessfulproto notify(Str , Str )
{*}
always dispatches to candidates with the parameters it's called with. Parameter defaults and type coercions will work but are not passed on.
proto mistake-proto(Str() , Int = 42)multi mistake-proto(, )mistake-proto(7, 42); # OUTPUT: «Int» -- not passed on
mistake-proto('test'); # fails -- not passed on