Liste des Groupes | Revenir à cl forth |
Ruvim <ruvim.pinka@gmail.com> writes:
I've compared the use of a to-based setter ("to foo") versus a separate setter ("set-foo") in programs. (NB: not creating them, but only using)What advantages do you see in *using* the to-based setters?Over using SET-... words?
1) It's a popular and convenient mechanism.Agreed. But this says about knowledge and tools, not about using already defined setters in the source code.
2) I can find out the setter from looking at the central word:A separate setter for a word "foo" can be checked using only standard words as follows:
``vf .hm[...]
\ output:
opt: $7F43EB229B98
to: $7F43EB229BE0
extra: $7F43EB229B60int: noopOk, $7F43EB229BE0 is not very informative, but I can still make use of
comp: default-name>comp
string: named>string
link: named>link ok
that:
$7F43EB229BE0 xt-locate
struct-val.fs:88
...
cell ' aligned ' @ !a-table wrap+value: value: ( u1 "name" -- u2 ) \ gforth-experimental
...
[...]For example, it could be a defining word "val" that is used as "val x"
and creates the getter "x" and the setter "set-x".
Thank you! As I see it, this interface is quite cumbersome for the specified problem, because it forces us to use two intermediate data objects: "to-table" and "to-class".\ Let's redefine the setter "set-x": to-x ( u xt -- )
: set-x ( u -- )
dup 10 u> abort" too big value for x"
set-x
;
>
t{ 4 set-x x -> 4 }t
t{ 11 ' set-x catch 2drop x -> 4 }t
>
>
Could you show how to implement that in Gforth when the to-based setter
"to x" is used?
>r dup 10 u> abort" too big value for x" r> >body ! ;
to-table: x-table to-x
' >body x-table to-class: x-to
0 value x
' x-to set-to
4 to x x . \ prints "4"
11 to x
The last line produces the following error:
*the terminal*:13:7: error: too big value for x
11 to >>>x<<<
Yes, for a bounds-checking TO you probably will define checking at once, or redefine the word at once in the same word list.NB: the solution must not change the original "x", it must redefine "x".Let's see. To make it easier to see what is going on I use a
The difference is that the new definition *can* be in a different word
list, in which case both definitions (old and new) can be used depending
on the context.
different name for the redefined word.
0 value y
synonym z y
' x-to set-to
4 to y
z . \ prints 4
11 to y
z . \ prints 11
11 to z \ aborts with "error: too big value for x"
For a bounds-checking TO I find this requirement strange, however.
Les messages affichés proviennent d'usenet.