Reputation:
I want to pass a parameter to a method which may only have a predefined value.
method send-http($url, $http_method) { .... }
Should I create an enum to pass $http_method
? If so then how?
Or does Perl 6 have something like symbols in Ruby?
Upvotes: 4
Views: 637
Reputation: 833
As @Christoph mentioned, you can use enums:
enum Method <GET PUT POST>;
sub http-send(str $url, Method $m) { * }
http-send("http://url/", GET);
You can also use type constraints:
sub http-send(str $url, str $m where { $m ∈ <GET HEAD POST> }) { * }
http-send("http://url/", 'GET');
http-send("http://url/", 'PUT');
Constraint type check failed for parameter '$m'
I guess you could also 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.
Upvotes: 3