to
and defer@
¶Gforth’s values have several operators: to
(to which is
is
an alias, and defer!
takes an xt instead of a name in the input
stream), +to
, addr
, and action-of
(where
defer@
takes an xt instead of a name in the input stream).
Gforth allows you to change the (to)
action of a word.
(to)
( val operation xt – ) gforth-1.0 “paren-to”
xt is of a value like word name. Stores val to
name. operation selects between to
, +to
,
addr
, and action-of
.
to-table:
( "name" "xt1" .. "xtn" – ) gforth-experimental “to-table-colon”
create a table with entries for TO
, +TO
,
ADDR
, and ACTION-OF
. Use n/a
to mark
unsupported operations.
to-method:
( xt table "name" – ) gforth-experimental “to-method-colon”
create a to-method, where xt computes the address to access the field, and table contains the operators to store to it.
set-to
( to-xt – ) gforth-1.0 “set-to”
Sets the implementation of the (to) ( val xt -- )
method
of the current word to to-xt.
n/a
( – ) gforth-experimental “not-available”
this word can be ticked, but throws an “Operation not supported” exception on interpretation and compilation. Use this for methods and alike that aren’t supported.
(to)
is a word used inside to
: it stores the value at
run-time. The general stack effect of (to)
method is (
val operation xt -- )
, where xt identifies the word stored into,
operation the actual variant of to
-like operations, and
val is the value (of appropriate type) stored there.
You shall implement the to
-method using to-table:
to
create a type-specific table of operations (not specified slots at the
end of the table are filled up with n/a
), and using
to-method:
together with the operation to get from the xt of
the value to its data field (usually >body
for values in the
dictionary, but value-style data can also reside in structures or user
areas).
E.g., one can implement fvalue
as follows:
to-table: f!-table f! f+! ' >body f!-table to-method: fvalue-to : fvalue ( r "name" -- ; name: -- r ) create f, ['] f@ set-does> ['] fvalue-to set-to ; 5e fvalue foo : bar foo 1e f+ to foo ; see bar