perl6 - Enum or symbols in Perl 6 -
i want pass parameter method may have predefined value.
method send-http($url, $http_method) { .... }
should create enum pass $http_method
? if how?
or perl 6 have symbols in ruby?
as @christoph mentioned, can use enums:
enum method <get put post>; sub http-send(str $url, method $m) { * } http-send("http://url/", get);
you can use type constraints:
sub http-send(str $url, str $m { $m ∈ <get head post> }) { * } http-send("http://url/", 'get'); http-send("http://url/", 'put'); constraint type check failed parameter '$m'
i guess use multi dispatch:
multi sub http-send('get') { * } multi sub http-send('put') { * } multi sub http-send($m) { die "method {$m} not supported." } http-send('get'); http-send('post'); method post not supported.
Comments
Post a Comment