REBOL 3 Docs Guide Concepts Functions Datatypes Errors
  TOC < Back Next >   Updated: 20-Nov-2013 Edit History  

REBOL 3 Guide: Code: Words and variables

Words are the key to REBOL.

Contents

Meaning of words

Words are commonly used in both code and data.

In code, words can be variables that holds values (including functions), but they can also be just symbols.

For example, here we use the word join three different ways:

join a b
actions: [split join copy]
get 'join

The first is using join as a function value, but the second and third are using join as a symbol.

Basically, the meaning of a word is defined by its:

Symbolthe name itself. So, obviously, join is not the same as ajoin. Different name.
Notationspecial markup on the word. Above 'join is a symbol, not a function variable.
Contexthow the word is associated with a value. The word may have a global or local meaning, or could be part of another context such as a module or object.
Usagehow the word is used in the code or data. Above, the word join in the actions block written as a symbol. Later it may be used to obtain the join function, but we do not know from this line, nor does it matter here.

Each of these will be explained below.

Special notations for words

A word can be written in several ways. Special notations can be used.

We want to introduce these now, so you can start to recognize them in the examples that follow throughout this document.

In essence, these notations modify the action or meaning related to the word.

Notation Definition Notes
word Get the natural value of the word. If the value is a function, evaluate it, otherwise return it.
word: Set the word to a value. We do not use = for assignment!
:word Get the value of a word without evaluating it. Useful for getting the value of a function.
'word Use the word as a symbol. We treat it as is and do not evaluate it.
/word Use the word as a refinement symbol. For functions, objects, file-paths, special fields.

Most of the time, you will use the first two, the natural notation and the set-word notation. But, the other notations are useful too, and you need to know them.

Words as symbols

REBOL is a symbolic language. You need to think of words as symbols of their own right, not just as variables, keywords, commands, or functions.

Above we wrote the line:

actions: [split join copy]

This block is not evaluated, so the words split, join, and copy are symbols.

We might use it in code like:

action: 'join
...
if find actions action [do something]

A lot more will be said about words as symbols, because they are widely used in REBOL for creating dialects (domain specific languages).

Words as variables

Variables are words with values associated within a specific context, such as in an object, module, or a function.

To set the value of a variable, this notation is used:

size: 12

You can now write this line to get the value of the variable:

print size
12

A lot more needs to be explained about variables, and it will be discussed later in this guide as we explain code: defining functions and data: objects provide context.

Why a colon?

We use colon for setting variables, not an equal (=) operator because it allows the language to easily recognize and manipulate variable definitions. This is very important to the language, and the reasons will become clear as you learn more.

Words for refinements

Words are also used for refinements.

A refinement! specifies a variation, option, or clarification in meaning. It is an abstraction that applies to functions, objects, indexing, file paths, and other data types.

For example, the copy function normally copies it's entire input:

print copy "example string"

But, when the /part refinement is provided, a partial copy is made:

print copy/part "example string" 7

In this way, we don't need another function for partial copies. We can have a single copy function with a number of refinements. This makes it easier to remember.

Examples of refinements for objects are similar. They are used to select fields of an object. You will be seeing this a lot.

print system/options/home
%/users/carl

That example finds the home field in the options object of the system object.

Why slash and not dot?

In REBOL, we write:

system/options/home

not:

system.options.home

We will admit that this was a difficult design decision. We did not make it lightly.

On the surface, the slash (/) was used for refinements because they are similar to file directory paths.

file-dir: %user/data
names: load file-dir/names.r

Also, slash refinements do a lot more than dot (selectors) in other languages, as can be seen in this line:

load/header/only system/options/home/database.r

Here, the load is being refined, and an object path is both accessed and filename appended.

As you can see, we also did not use dot because we didn't want to lose the ability to specify file names as refinements (with suffixes that use dot, like that shown above).

Of course, this is a good time to remind you that all words and values in REBOL must be separated by spaces.

So, divide is:

m: n / 2

but:

m: n/2

is an indexing refinement to n (assuming it is a series).

Context provides meaning

For a word to have meaning, its context must be known.

That context can be the meaning of the word:

Let's clarify this with three examples.

First, take this example:

sizes: [large medium small]
print sizes
large medium small

The words large, medium, and small are just symbols. Defined this way, they are not variables, and they have no meaning to REBOL, only meaning to you, the reader.

Second, look at this:

large:  1000
medium: 100
small:  10
print [large medium small]
1000 100 10

Here, the words have been defined to have specific values within the scope that is known when the print function is evaluated by REBOL. These words are used as variables.

And, the third example is:

rules: ['data 'is ['large | 'medium | 'small]]
parse [data is medium] rules
true
parse [data is huge] rules
false

As in the first example, the words large, medium, and small are symbols, but their meaning is defined by the rules provided to parse. Their meaning is relative to those rules. They define a dialect of the language (although a trivial one at that.)

Usage changes the meaning

The meaning of words can also vary depending on how they are used. Like verbs and nouns, sometimes a verb can be a noun, and vice versa.

For example, these lines all use the same word, print, but it's meaning is different for each.

print 123
probe [print]
source print
do get second [probe print] 456
parse [time to print 10:30] [thru 'print set time time!]
database/print data

Each of these uses print in a different way.


  TOC < Back Next > REBOL.com - WIP Wiki Feedback Admin