Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
34
Task/Constrained-genericity/Wren/constrained-genericity.wren
Normal file
34
Task/Constrained-genericity/Wren/constrained-genericity.wren
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
// abstract class
|
||||
class Eatable {
|
||||
eat() { /* override in child class */ }
|
||||
}
|
||||
|
||||
class FoodBox {
|
||||
construct new(contents) {
|
||||
if (contents.any { |e| !(e is Eatable) }) {
|
||||
Fiber.abort("All FoodBox elements must be eatable.")
|
||||
}
|
||||
_contents = contents
|
||||
}
|
||||
|
||||
contents { _contents }
|
||||
}
|
||||
|
||||
// Inherits from Eatable and overrides eat() method.
|
||||
class Pie is Eatable {
|
||||
construct new(filling) { _filling = filling }
|
||||
|
||||
eat() { System.print("%(_filling) pie, yum!") }
|
||||
}
|
||||
|
||||
// Not an Eatable.
|
||||
class Bicycle {
|
||||
construct new() {}
|
||||
}
|
||||
|
||||
var items = [Pie.new("Apple"), Pie.new("Gooseberry")]
|
||||
var fb = FoodBox.new(items)
|
||||
fb.contents.each { |item| item.eat() }
|
||||
System.print()
|
||||
items.add(Bicycle.new())
|
||||
fb = FoodBox.new(items) // throws an error because Bicycle not eatable
|
||||
Loading…
Add table
Add a link
Reference in a new issue