Comments on: Read-only strings, blocks, and objects in A43

Carl Sassenrath, CTO
REBOL Technologies
4-Apr-2009 18:25 GMT

Article #0186
Main page || Index || Prior Article [0185] || Next Article [0187] || 6 Comments || Send feedback

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.



Peter Wood
4-Apr-2009 20:53:52
Having protected constants is a great addition. Have you considered providing a /locked refinement which would stop the protection being later reversed?
Anton Rolls
5-Apr-2009 5:05:06
I want to be the first to try:

protect/deep system

5-Apr-2009 5:17:48
Protect is a good name for modules having 'import, 'export, 'protect. Such protected values should not be even readable from outside word, not protect from changes. With OOP I used in the past, exactly 'protect word was used. But not sure it would not confuse users, as 'protected value can be unprotected and does not prevent you from reading :-) Maybe 'hide is another candidate for such a word ...
5-Apr-2009 9:33:48
'freeze seems more descriptive than 'protect. The function is very usefull in some contexts. And 'freeze semantics does not overlaps with 'hide.
Carl Sassenrath
5-Apr-2009 22:13:40
Peter, yes... having some way to prevent unprotect needs to be added. Perhaps a /lock refinement would make them irreversible like that.
Carl Sassenrath
5-Apr-2009 22:14:41
Pekr: with /hide added now, we're getting there. Needs now to be added to the IMPORT mezz function.

Post a Comment:

You can post a comment here. Keep it on-topic.


Blog id:



 Note: HTML tags allowed for: b i u li ol ul font span div a p br pre tt blockquote

This is a technical blog related to the above topic. We reserve the right to remove comments that are off-topic, irrelevant links, advertisements, spams, personal attacks, politics, religion, etc.

Updated 25-Jun-2024 - Edit - Copyright REBOL Technologies -