REBOL Programming/Language Features/Objects
From Wikibooks, the open-content textbooks collection
Contents |
[edit] Objects
Objects are a very common way to manage and access data in REBOL. In fact most of the REBOL system is built with objects. The SYSTEM object can be considered the spine of REBOL, the place where all non-core functions and data reside. It's the place you can find all of VID, the various networking protocols and all mezzanine functions.
[edit] Using Objects
Objects are made with the MAKE function.
Example:
>> shopping-basket: make object! [fruits: 5 vegetables: 3] >>
Note that nothing is returned on the console. Objects can hold all types of data, even other objects.
A bigger example, as you'd normally write it in your program code:
shopping-basket: make object! [ fruits: 5 vegetables: 3 dairy-products: 7 ]
[edit] Accessing Objects
If you want to view an object, you might have noticed that typing the name in doesn't return anything:
>> shopping-basket >>
You can use different methods for viewing the object data:
>> probe shopping-basket make object! [ fruits: 5 vegetables: 3 ]
A nicer overview can be returned with:
>> ? shopping-basket SHOPPING-BASKET is an object of value: fruits integer! 5 vegetables integer! 3 total integer! 8
Or:
>> print dump-obj shopping-basket fruits integer! 5 vegetables integer! 3 total integer! 8
These functions can't be used in programs to get values from an object, since they only print information in the console. They don't produce a return value.
One way is to use path notation:
>> shopping-basket/fruits == 5
There are multiple ways to access object data, both for manipulation and viewing.
[edit] Referring to Self
An object can refer to other variables and functions within itself using SELF:
>> shopping-basket: make object! [fruits: 5 vegetables: 3 total: self/fruits + self/vegetables] >> shopping-basket/total == 8
[edit] Practical Use of Objects
So we've now learned how to build an object with data and values. What is then so cool about them?
[edit] Binding
This is hard... who wants to write about this? :-)

