Constrained genericity: Difference between revisions

(→‎{{header|C++}}: Concepts no longer in C++0x)
Line 229:
return [].diverge(Eatable) # A guard-constrained list
}</lang>
 
=={{header|F_Sharp|F#}}==
It is possible to constrain type parameters in a number of ways, including inheritance relationships and interface implementation. But for this task, the natural choice is an explicit member constraint.
<lang fsharp>type ^a FoodBox // a generic type FoodBox
when ^a: (member eat: unit -> string) // with an explicit member constraint on ^a,
(items:^a list) = // a one-argument constructor
member inline x.foodItems = items // and a public read-only property
 
// a class type that fullfills the member constraint
type Banana() =
member x.eat() = "I'm eating a banana."
 
// an instance of a Banana FoodBox
let someBananas = FoodBox [Banana(); Banana()]</lang>
 
=={{header|Haskell}}==
Anonymous user