Portál AbcLinuxu, 1. května 2025 22:23
subset Filename of Str where { $_ ~~ :f };
, a tak jsem jedním dotazem do Googlu narazil na zápisek, který se tentýž problém snaží aspoň trochu vysvětlovat: http://waffle.wootest.net/2009/12/19/perl-6-feature-ep1/. Oproti němu je přítomný text opravdu prachbídný. Předpokládám, že :f
je tedy role "býti existujícím souborem"? Pobavilo mne mimochodem, že parametr $name
ve funkci foo
má staticky deklarovaný typ role "býti existujícím souborem"Tedy "býti názvem existujícího souboru", když je to řetězec.
multi sub fib (Int $n where 0|1) { return $n } multi sub fib (Int $n) { return fib($n-1) + fib($n-2) }…případně:
multi sub fib (0) { return 0 } multi sub fib (1) { return 1 } multi sub fib (Int $n) { return fib($n-1) + fib($n-2) }To je pěkné, vzory jsem Haskellu vždycky záviděl :)
Multiple dispatch is based on the notion that methods often mediate the relationships of multiple objects of diverse types, and therefore the first object in the argument list should not be privileged over other objects in the argument list when it comes to selecting which method to run.Kažpodádně demonstracë multiple dispatch na funkci jedné proměnné mi pořád přijde zvláštní.
The order in which candidates are considered is defined by a topological sort based on the "type narrowness" of each candidate's long name, where that in turn depends on the narrowness of each parameter that is participating.…plus několik dalších odstavců textu.
Thus, constrained signatures are considered to be much more like a switch defined by the user. So for tiebreaker A the candidates are simply called in the order they were declared, and the first one that successfully binds (and completes without calling nextsame or nextwith) is considered the winner, and all the other tied candidates are ignored.
subset Odd of Int where { $_ % 2 };I typ
Filename
se ale může hodit, třeba v menších skriptech. Důležité je, aby si člověk uvědomoval jeho omezení.
$ perl6 -e ' multi sub foo($n where :f) { say "OK $n" } multi sub foo($n) { say "NAAH $n" } foo("/bin/pwd"); foo("/bagr"); ' OK /bin/pwd NAAH /bagrKazdopadne krasna featura :)
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.