RosettaCodeData/Task/Constrained-genericity/Forth/constrained-genericity.fth

40 lines
849 B
Forth
Raw Permalink Normal View History

2015-11-18 06:14:39 +00:00
include FMS-SI.f
include FMS-SILib.f
:class Eatable
2017-09-23 10:01:46 +02:00
:m eat ." successful eat " ;m
2015-11-18 06:14:39 +00:00
;class
2017-09-23 10:01:46 +02:00
\ FoodBox is defined without inspecting for the eat message
2015-11-18 06:14:39 +00:00
:class FoodBox
object-list eatable-types
2017-09-23 10:01:46 +02:00
:m init: eatable-types init: ;m
:m add: ( obj -- )
dup is-kindOf Eatable
if eatable-types add:
else drop ." not an eatable type "
2015-11-18 06:14:39 +00:00
then ;m
2017-09-23 10:01:46 +02:00
:m test
begin eatable-types each:
while eat
repeat ;m
2015-11-18 06:14:39 +00:00
;class
2017-09-23 10:01:46 +02:00
FoodBox aFoodBox
Eatable aEatable
aEatable aFoodBox add: \ add the e1 object to the object-list
aFoodBox test \ => successful eat
2015-11-18 06:14:39 +00:00
2017-09-23 10:01:46 +02:00
:class brick
:m eat cr ." successful eat " ;m
;class
2015-11-18 06:14:39 +00:00
2017-09-23 10:01:46 +02:00
brick abrick \ create an object that is not eatable
abrick aFoodBox add: \ => not an eatable type
2015-11-18 06:14:39 +00:00
:class apple <super Eatable
;class
2017-09-23 10:01:46 +02:00
apple anapple
anapple aFoodBox add:
aFoodBox test \ => successful eat successful eat