Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
|
|
@ -0,0 +1,27 @@
|
|||
import morfa.type.traits;
|
||||
|
||||
template < T >
|
||||
alias IsEdible = HasMember< T, "eat" >;
|
||||
|
||||
template < T >
|
||||
if (IsEdible< T >)
|
||||
struct FoodBox
|
||||
{
|
||||
var food: T[];
|
||||
}
|
||||
|
||||
struct Carrot
|
||||
{
|
||||
func eat(): void {}
|
||||
}
|
||||
|
||||
struct Car {}
|
||||
|
||||
func main(): void
|
||||
{
|
||||
var carrotBox: FoodBox< Carrot >; // OK
|
||||
carrotBox.food ~= Carrot(); // Adds a carrot
|
||||
|
||||
// var carBox: FoodBox< Car >; // Not allowed
|
||||
static assert( not trait(compiles, func() { var carBox: FoodBox< Car >; } ));
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
interface IEdible
|
||||
{
|
||||
public func eat(): void;
|
||||
}
|
||||
|
||||
template < T >
|
||||
if (IsDerivedOf< T, IEdible >)
|
||||
struct FoodBox
|
||||
{
|
||||
var food: T[];
|
||||
}
|
||||
|
||||
class Carrot: IEdible
|
||||
{
|
||||
public override func eat(): void {}
|
||||
}
|
||||
|
||||
class Car {}
|
||||
|
||||
func main(): void
|
||||
{
|
||||
var carrotBox: FoodBox< Carrot >; // OK
|
||||
|
||||
// var carBox: FoodBox< Car >; // Not allowed
|
||||
static assert( not trait(compiles, func() { var carBox: FoodBox< Car >; } ));
|
||||
}
|
||||
21
Task/Constrained-genericity/Nim/constrained-genericity.nim
Normal file
21
Task/Constrained-genericity/Nim/constrained-genericity.nim
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
type
|
||||
Eatable = generic e
|
||||
eat(e)
|
||||
|
||||
FoodBox[e: Eatable] = seq[e]
|
||||
|
||||
Food = object
|
||||
name: string
|
||||
count: int
|
||||
|
||||
proc eat(x: int) = echo "Eating the int: ", x
|
||||
proc eat(x: Food) = echo "Eating ", x.count, " ", x.name, "s"
|
||||
|
||||
var ints = FoodBox[int](@[1,2,3,4,5])
|
||||
var fs = FoodBox[Food](@[])
|
||||
|
||||
fs.add Food(name: "Hamburger", count: 3)
|
||||
fs.add Food(name: "Cheeseburger", count: 5)
|
||||
|
||||
for f in fs:
|
||||
eat(f)
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
class FoodBox(*food { .all { .respond_to(:eat) } }) { }
|
||||
|
||||
class Fruit { method eat { ... } }
|
||||
class Apple < Fruit { }
|
||||
|
||||
say FoodBox(Fruit(), Apple()).dump #=> FoodBox(food: [Fruit(), Apple()])
|
||||
say FoodBox(Apple(), "foo") #!> ERROR: class `FoodBox` !~ (Apple, String)
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
protocol Eatable {
|
||||
func eat()
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
struct FoodBox<T: Eatable> {
|
||||
var food: [T]
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
func foo<T: Eatable>(x: T) { }
|
||||
// although in this case this is no more useful than just "func foo(x: Eatable)"
|
||||
Loading…
Add table
Add a link
Reference in a new issue