Section 36
Want more of a challenge? View in
iconic
form (
experimental
)
# OBJECT an example object -- a container
[
hear
]
(define container
(lambda
(x)
(let
(
(contents
(make-cell
(vector)
)
)
)
(reflective
(lambda
(self msg)
(cond
(
(=
(msg)
container)
(self)
)
(
(=
(msg)
inventory)
(get!
(contents)
)
)
(
(=
(msg)
add)
(lambda
(x)
(if
(not
(element
(x)
(get!
(contents)
)
)
)
(set!
(contents)
(prepend
(x)
(get!
(contents)
)
)
)
(false)
)
)
)
(
(=
(msg)
remove)
(lambda
(x)
(set!
(contents)
(remove-element
(x)
(get!
(contents)
)
)
)
)
)
(
(=
(msg)
=)
(lambda
(
(c container)
)
(set=
(self inventory)
(c inventory)
)
)
)
0)
)
)
)
)
);
# Can pass anything to container function to create an object
# Should eventually use a consistent protocol for all objects,
# but all this stuff is still in flux
[
hear
]
(define pocket
(container new)
);
[
hear
]
(pocket add 77);
[
hear
]
(pocket add 88);
[
hear
]
(pocket add 99);
[
hear
]
(set=
(pocket inventory)
(vector 77 88 99)
);
[
hear
]
(pocket remove 88);
[
hear
]
(set=
(pocket inventory)
(vector 77 99)
);
[
hear
]
(define pocket2
(container new)
);
[
hear
]
(pocket2 add 77);
[
hear
]
(pocket2 add 99);
[
hear
]
(pocket2 =
(pocket)
);