Constrained genericity: Difference between revisions

added objc
(added objc)
Line 671:
for f in fs:
eat(f)</lang>
 
=={{header|Objective-C}}==
{{works with|Xcode|7}}
Type constraints are made on the type hierarchy, so here we make <code>Eatable</code> a protocol, with an <code>eat</code> method.
Types which are Eatable would have to implement the
<code>Eatable</code> protocol and provide an <code>eat</code> method.
<lang objc>@protocol Eatable
- (void)eat;
@end</lang>
Type constraints in type parameters can be made via the <code>:</code> keyword, indicating in this case that the type argument must be a type that is a subtype of <code>id<Eatable></code>.
<lang objc>@interface FoodBox<T : id<Eatable>>
@end</lang>
 
=={{header|OCaml}}==
Anonymous user