Sync
This commit is contained in:
parent
6f050a029e
commit
776bba907c
3887 changed files with 59894 additions and 7280 deletions
|
|
@ -1,6 +1,4 @@
|
|||
template IsEdible(T) {
|
||||
enum IsEdible = is(typeof(T.eat()));
|
||||
}
|
||||
enum IsEdible(T) = is(typeof(T.eat));
|
||||
|
||||
struct FoodBox(T) if (IsEdible!T) {
|
||||
T[] food;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,23 @@
|
|||
import Utils # From the UniLib package to get the Class class.
|
||||
|
||||
class Eatable:Class()
|
||||
end
|
||||
|
||||
class Fish:Eatable(name)
|
||||
method eat(); write("Eating "+name); end
|
||||
end
|
||||
|
||||
class Rock:Class(name)
|
||||
method eat(); write("Eating "+name); end
|
||||
end
|
||||
|
||||
class FoodBox(A)
|
||||
initially
|
||||
every item := !A do if "Eatable" == item.Type() then next else bad := "yes"
|
||||
return /bad
|
||||
end
|
||||
|
||||
procedure main()
|
||||
if FoodBox([Fish("salmon")]) then write("Edible") else write("Inedible")
|
||||
if FoodBox([Rock("granite")]) then write("Edible") else write("Inedible")
|
||||
end
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
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();
|
||||
Loading…
Add table
Add a link
Reference in a new issue