September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
|
|
@ -0,0 +1,23 @@
|
|||
class Apple
|
||||
def eat
|
||||
end
|
||||
end
|
||||
|
||||
class Carrot
|
||||
def eat
|
||||
end
|
||||
end
|
||||
|
||||
class FoodBox(T)
|
||||
def initialize(@data : Array(T))
|
||||
{% if T.union? %}
|
||||
{% raise "All items should be eatable" unless T.union_types.all? &.has_method?(:eat) %}
|
||||
{% else %}
|
||||
{% raise "Items should be eatable" unless T.has_method?(:eat) %}
|
||||
{% end %}
|
||||
end
|
||||
end
|
||||
|
||||
FoodBox.new([Apple.new, Apple.new])
|
||||
FoodBox.new([Apple.new, Carrot.new])
|
||||
FoodBox.new([Apple.new, Carrot.new, 123])
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
use Collection.Generic;
|
||||
|
||||
interface Eatable {
|
||||
method : virtual : Eat() ~ Nil;
|
||||
}
|
||||
|
||||
class FoodBox<T : Eatable> {
|
||||
food : List<T>;
|
||||
}
|
||||
|
||||
class Plum implements Eatable {
|
||||
method : Eat() ~ Nil {
|
||||
"Yummy Plum!"->PrintLine();
|
||||
}
|
||||
}
|
||||
|
||||
class Genericity {
|
||||
function : Main(args : String[]) ~ Nil {
|
||||
plums : FoodBox<Plum>;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue