Comments on: SET and GET on Objects
As I mentioned earlier, newer versions of REBOL will expand the SET and GET functions to work with objects. Here are the details.
Using SET on Objects
The SET function lets you set all the fields of an object to a single value:
set object value
but also allows each field to be set to a different value:
set object [value1 value2 value3 ...]
This second form lets you use objects in a manner that is similar to structures in languages like C. It is useful for reading records (blocks) from database files and quickly setting them to an object (to make field access easier).
For example, if you have the object:
person: make object! [ name: "Jane Doe" email: firstname.lastname@example.org age: 32 ]
You can set all of its fields to NONE with:
set person none
Or change all of its fields with:
set person ["Bob Smith" email@example.com 25]
In this last case, if you do not provide enough fields in the block, the remaining fields will not be changed. So, the line:
set person ["Bob Smith"]
Will only change the name field.
To change that behavior, you can use the /pad refinement to set all remaining fields to NONE. Now, the line:
set/pad person ["Bob Smith"]
This will set the first field to the string, but all other fields will be set to NONE. (Similar to how object fields are set when using MAKE.)
Using GET on Objects
Using the GET function on an object will return all the object's field values as a block. Using GET on the object defined above:
probe get person ["Jane Doe" firstname.lastname@example.org 32]
Post a Comment:
You can post a comment here. Keep it on-topic.