Doug Hoffman <no.spam> wrote:
> I think that if any "over hyping" of OOP has occurred it was because
> suddenly it gave users of languages other than Forth the power of a
> create/does> construct. I can see where people using other languages
> would become excited over suddenly having the power (and more) of
> Forth's create/does>.
Let's review again what CREATE DOES> does.
It lets you associate an action with an address. The action can be
re-used for many addresses. One to many. But the address can't easily be
associated with other actions, no many-to-one. Once an address has been
linked by DOES> it's actually harder to associate it with any other
action.
You can mimic DOES> easily without anything special, and get *most* of
the same results.
CREATE FOO-addr 5 CELLS ALLOT
: FOO
FOO-addr ACTION ;
This has an extra name, but the extra name lets you get to the base
address easily any time. It takes more typing to make a second one than
: DO-ACTION
CREATE CELLS ALLOT
DOES> ACTION ;
5 DO-ACTION FOO
You can reduce that.
: DOESIT ( xt addr "name" -- )
: postpone literal compile, postpone ; ;
HERE 5 CELLS ALLOT ' ACTION DOESIT FOO
and it could be reduced further.
: DOES-ONE ( "name" "action" -- )
HERE 1 CELLS ALLOT DUP >R
: \ start "name"
R> ]] LITERAL \ compile pointer address
@[EMAIL PROTECTED]
\ compile base address
[[ ] PARSE-NAME \ get action name
EVALUATE \ compile action
POSTPONE ; \ "name" definition done
HERE SWAP ! \ base address for data
;
DOES-ONE FOO ACTION 5 CELLS ALLOT
This isn't the same as DOES> because it restricts the DOES> .... ;
clause to a single name, and it creates its own new data field instead
of using the latest CREATEd field. In both cases the data field can be
as big as you like, where the earlier versions needed you to allot all
its space before you compiled another definition (at least when
dataspace and codespace or namespace are mixed).
Again, what DOES> gives you isn't that much. It's simple to use DOES> so
that you can have an array, and instead of doing
FOO 5 TH instead you do
5 FOO
You can have a complicated data structure and instead of doing
FOO ( addr ) SPECIAL-DATA-FIELD ( addr')
you can do
SPECIAL-DATA-FIELD ( token ) FOO ( addr')
I'm sure there are times when this is vitally im****tant.
It's possible to get these results in a way that's both simpler and more
powerful than CREATE DOES> . But we don't need the simplicity,
particularly when we have such a long tradition with CREATE DOES> . And
we may not have any use for the additional power.


|