Constrained genericity: Difference between revisions

no edit summary
No edit summary
Line 406:
<lang forth>include FMS-SI.f
include FMS-SILib.f
 
: (where) ( class-xt where-dfa -- flag )
swap >body { where-dfa class-dfa }
begin
class-dfa ['] object >body <>
while
class-dfa where-dfa = if true exit then
class-dfa sfa @ to class-dfa
repeat false ;
 
: where ( class-xt "classname" -- flag )
' >body state @
if postpone literal postpone (where)
else (where)
then ; immediate
 
:class Eatable
:m eat cr ." successful eat " ;m
;class
 
\ FoodBox is defined without usinginspecting eatfor inthe anyeat way.message
:class FoodBox
object-list eatable-types
:m init: eatable-types init: ;m
:m filladd: {( n class-xtobj -- })
class-xtdup whereis-kindOf Eatable
if n 0 do class-xt eatable-types xtaddadd: loop
else drop ." not an eatable type "
then ;m
fb1 get :m test
:m get ( -- obj )begin eatable-types ;m each:
while eat
repeat false ;m
;class
FoodBox fbaFoodBox
Eatable aEatable
aEatable aFoodBox add: \ add the e1 object to the object-list
aFoodBox test \ => successful eat
 
;:class brick
: test ( obj -- ) \ send the eat message to each object in the object-list
:m eat cr ." successful eat " ;m
begin dup each:
;class
while eat
repeat drop ;
 
brick abrick \ create an object that is not eatable
FoodBox fb
5abrick 'aFoodBox object fb1 filladd: \ => not an eatable type
3 ' Eatable fb fill: \ fill the object-list with 3 objects of class Eatable
fb get test
successful eat
successful eat
successful eat
 
FoodBox fb1
5 ' object fb1 fill: \ => not an eatable type
 
:class apple <super Eatable
;class
 
:class green-apple <superanapple apple
anapple aFoodBox add:
;class
aFoodBox test \ => successful eat successful eat
 
5 ' green-apple fb1 fill:
fb1 get test
successful eat
successful eat
successful eat
successful eat
successful eat
</lang>