RosettaCodeData/Task/Constrained-genericity/Nemerle/constrained-genericity.nemerle
Ingy döt Net 776bba907c Sync
2013-10-27 22:24:23 +00:00

46 lines
726 B
Text

using System.Collections.Generic;
interface IEatable
{
Eat() : void;
}
class FoodBox[T] : IEnumerable[T]
where T : IEatable
{
private _foods : list[T] = [];
public this() {}
public this(items : IEnumerable[T])
{
this._foods = $[food | food in items];
}
public Add(food : T) : FoodBox[T]
{
FoodBox(food::_foods);
}
public GetEnumerator() : IEnumerator[T]
{
_foods.GetEnumerator();
}
}
class Apple : IEatable
{
public this() {}
public Eat() : void
{
System.Console.WriteLine("nom..nom..nom");
}
}
mutable appleBox = FoodBox();
repeat(3) {
appleBox = appleBox.Add(Apple());
}
foreach (apple in appleBox) apple.Eat();