Read-only strings, blocks, and objects in A43
R3 A43 extends the protect function. It can now protect series as well as variables from modification. This lets you make values like strings read-only, turning them into protected constants.
For full details, check out the protect function in the new R3 Doc wiki.
Take these values:
text: "string"
block: [1 test "here"]
obj: object [name: "Bob" age: 32]
You can now write:
protect text
protect block
protect obj
and, each will be protected from modification.
append text "a"
** Script error: protected value or series - cannot modify
append block 1
** Script error: protected value or series - cannot modify
append obj [city "Ukiah"]
** Script error: protected value or series - cannot modify
obj/name: "Ted"
** Script error: protected variable - cannot modify: name
However, protect by default does not protect series within series.
So, in the block, the "here" string can still be modified, and in the object, the name can be modified.
To protect deeply, use:
protect/deep block
protect/deep obj
Now, you cannot modify any series (string or block) within those.
In addition, protect can still protect words used as varaibles:
protect 'text
But the block form of protecting multiple words must use the /words refinement:
protect/words [text block obj]
And it's not the same as:
protect [text block obj]
or, the quite useful form (which will protect all of the above series and their contents too any level):
protect/deep [text block obj]
This semantic change was required because protect of a block now means to protect the block series itself, not its contents. (I debated this change for quite a while, but sometimes you just need to be bold and push forward.)
Note that the unprotect function works exactly the same as protect, but in reverse.
6 Comments
|