have perl6 invoke the right multi sub specialized by subtype(subset)

  • A+
Category:Languages

I have a type hierarchy constructed with perl6's subset command and some multi subs specialized on these type. How to give the highest precedence to the sub specialized by the most narrow subtype when multi dispatch happens?

Here is the simplified code:

#! /usr/bin/env perl6  use v6.c;  proto check($value) { * }  subset Positive of Int where * > 0;  subset PositiveEven of Positive where * %% 2;  multi check(Int $value) {     say "integer" }  multi check(Positive $value) {     say "positive" }  multi check(PositiveEven $value) {     say "positive & even" }  # example: check(32);  # expected output: #   positive & even  # actual output: #   positive  

 


Since all candidates are equally tight, it will take the first one that matches the rest of the constraint (or lack thereof). This is when the order in which the multi candidates are specified, becomes important. If you would have specified them in this order:

multi check(PositiveEven $value) { say "positive & even" } multi check(Positive $value) { say "positive" } multi check(Int $value) { say "integer" } 

The following will do what you expect:

check(32);   # positive & even 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: