Constrained genericity: Difference between revisions

→‎{{header|Perl 6}}: This time fer shure! --Bullwinkle
(→‎{{header|Ada}}: I think this solves the issue)
(→‎{{header|Perl 6}}: This time fer shure! --Bullwinkle)
Line 336:
let your_box = FloatBox.make_box_from_list [2.3; 4.5]</lang>
Unfortunately, it is kind of cumbersome in that, for every type parameter we want to use for this generic type, we will have to explicitly create a module for the resulting type (i.e. <tt>BananaBox</tt>, <tt>FloatBox</tt>). And the operations on that resulting type (i.e. <tt>make_box_from_list</tt>) are tied to each specific module.
=={{header|Perl 6}}==
{{incorrect|Perl 6|While now the conditioning on existence of eat function seems to be right, now instead of a generic type which can be instantiated for specific food types, there's one single foodbox variable where any type of food can be put in.}}
 
{{works with|Rakudo|2010.09.17}}
<lang perl6>
<lang perl6>subset Eatable of Any where { .^can('eat') };
 
my Eatable @foodbox;
class Cake { method eat() {...} }
</lang>
 
role FoodBox[Eatable ::T] {
has T %.foodbox;
}
 
class Yummy does FoodBox[Cake] { } # composes correctly
# class Yucky does FoodBox[Int] { } # fails to compose
 
my EatableYummy @$foodbox .= new;
say $foodbox.perl;</lang>
 
Output:
<lang>Yummy.new(foodbox => {})</lang>
 
=={{header|PicoLisp}}==
Anonymous user