Comments on: SET and GET on Objects
REBOL Technologies

Comments on: SET and GET on Objects

Carl Sassenrath, CTO
REBOL Technologies
14-Apr-2005 22:52 GMT

Article #0153
Main page || Index || Prior Article [0152] || Next Article [0154] || Post Comments || Send feedback

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: jane.doe@example.com
    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" bob@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.)

Important notes about SET:

The SET object operation was designed to be very efficient and you should be aware of these issues:

  1. The SET action does not bind words into the context of the object. So, words do not become local to the object. Be careful using SET for functions, blocks, and other values that are sensitive to object context.
  2. Series values are not copied. This is an intentional design choice. We optimized the use of SET for uniquely loaded blocks (such as those from a database file). If you need to make copies, use COPY/DEEP on the block prior to the SET function.
  3. The internal SELF field is not modified by the SET function.

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" jane.doe@example.com 32]
Important notes about GET:

The GET object operation was designed go be very efficient and you should be aware of these issues:

  1. The GET action does not change the binding of words. If they are bound to the object, they will continue to be bound to the object. Be careful using GET for functions, blocks, and other values that are sensitive to object context.
  2. Series values are not copied. This is an intentional design choice. We optimized the use of GET for writing to an output file. If you need copies, use COPY/DEEP on the block after the GET function.
  3. The internal SELF field is not returned as part of the GET function.

Post Comments

Post a Comment:

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

Name:

Blog id:

CS-0153


Comment:


 Note: HTML tags allowed for: b i u li ol ul font 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-Nov-2017   -   Copyright Carl Sassenrath   -   WWW.REBOL.COM   -   Edit   -   Blogger Source Code