[REBOL] Objects Re:
From: bhandley:zip:au at: 21-Aug-2000 11:15
Objects provide a way to group a set of values into a context that can be
passed around as a whole and treated as a single value. This is useful for
dealing with structures that are more complex in nature, as they allow the
data and code to be kept together (encapsulated).
Once created, an object can be treated as a single value. It can be passed
to a function as an argument, returned from a function as a result, or set
to a word. The values within the object are accessed by name.
That was from the user guide.
Objects help to structure scripts.
Think of how useful local variables are in functions. You create a local
variable, use it and know that it will not clash with words outside of the
function*. Objects give you local variables and local functions packaged
together into one blob. Now that is useful. The local functions can see the
local variables and the other local functions and be like a mini-program
embedded inside a bigger one.
Before I get clouted for using confusing terminology - the guide refers to
these as "fields" or "instance variables" and "object functions".
Here's an example, when I've been programming using the parse function, I
can end up with quite a few parse rules and other variables related to
parsing. It might be that I want my parsing functionality to be part of a
bigger script - so it would be good to put everything related to parsing
(rules, flags, functions) in one spot - an object. Doing this makes my
overall script more readable since I know that everything in that particular
object relates to the parsing functionality. Also, it simplifies the my
naming of words and prevents naming clashes with global words. It sort of
like having work areas of a factory floor painted with coloured safety lines
in order to separate hazardous machines or processes.
As the guide mentions another use for objects is for structuring your data.
You can create a special data structure with functions to manipulate it and
put these in an object. So you can have object functions that add and remove
values from your data structure and be comforted to know that the object is
making sure the structure is always mainted properly.
Brett.
* Unless you have done something quite tricky - but let's leave that for
now.