Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
24
Task/Constrained-genericity/Ada/constrained-genericity-1.adb
Normal file
24
Task/Constrained-genericity/Ada/constrained-genericity-1.adb
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
with Ada.Containers.Indefinite_Vectors;
|
||||
|
||||
package Nutrition is
|
||||
type Food is interface;
|
||||
procedure Eat (Object : in out Food) is abstract;
|
||||
|
||||
end Nutrition;
|
||||
|
||||
with Ada.Containers;
|
||||
with Nutrition;
|
||||
|
||||
generic
|
||||
type New_Food is new Nutrition.Food;
|
||||
package Food_Boxes is
|
||||
|
||||
package Food_Vectors is
|
||||
new Ada.Containers.Indefinite_Vectors
|
||||
( Index_Type => Positive,
|
||||
Element_Type => New_Food
|
||||
);
|
||||
|
||||
subtype Food_Box is Food_Vectors.Vector;
|
||||
|
||||
end Food_Boxes;
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
type Banana is new Food with null record;
|
||||
overriding procedure Eat (Object : in out Banana) is null;
|
||||
package Banana_Box is new Food_Boxes (Banana);
|
||||
|
||||
type Tomato is new Food with null record;
|
||||
overriding procedure Eat (Object : in out Tomato) is null;
|
||||
package Tomato_Box is new Food_Boxes (Tomato);
|
||||
-- We have declared Banana and Tomato as a Food.
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
-- Abstract class.
|
||||
class eatable
|
||||
function eat() end -- override in child class
|
||||
end
|
||||
|
||||
class foodbox extends eatable
|
||||
private contents
|
||||
|
||||
function __construct(contents)
|
||||
if !contents:checkall(|c| -> c instanceof eatable) then
|
||||
error("All foodbox elements must be eatable.")
|
||||
end
|
||||
self.contents = contents
|
||||
end
|
||||
|
||||
function getContents() return self.contents end
|
||||
end
|
||||
|
||||
-- Inherits from eatable and overrides eat() method.
|
||||
class pie extends eatable
|
||||
function __construct(private filling) end
|
||||
|
||||
function eat()
|
||||
print($"{self.filling} pie, yum!")
|
||||
end
|
||||
end
|
||||
|
||||
-- Not an eatable
|
||||
class bicycle end
|
||||
|
||||
local items = {new pie("Apple"), new pie("Gooseberry")}
|
||||
local fb = new foodbox(items)
|
||||
fb:getContents():foreach(|item| -> item:eat())
|
||||
print()
|
||||
items:insert(new bicycle())
|
||||
fb = new foodbox(items) -- throws an error because bicycle not eatable
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
interface Eatable {
|
||||
eat()
|
||||
}
|
||||
|
||||
type Foodbox = []Eatable
|
||||
|
||||
type Peelfirst = string
|
||||
|
||||
fn (f Peelfirst) eat() {
|
||||
// peel code goes here
|
||||
println("mm, that ${f} was good!")
|
||||
}
|
||||
|
||||
fn main() {
|
||||
mut fb := Foodbox{}
|
||||
fb << [Peelfirst("banana"), Peelfirst("mango")]
|
||||
// fb would print as `[Eatable(Peelfirst(banana)), Eatable(Peelfirst(mango))]`
|
||||
f0 := fb[0]
|
||||
f0.eat()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue