REBOL 3 Docs Guide Concepts Functions Datatypes Errors
  TOC < Back Next >   Updated: 1-Aug-2009 Edit History  

REBOL 3 Datatypes: Word!

Contents

Concept

Words are the symbols used by REBOL. A word may or may not be a variable, depending on how it is used. Words are quite often used directly as symbols, rather than variables. It is important to understand the difference.

REBOL does not use keywords -- those specific words that can only be used in one way.

For example, in C code, an if statement may be written:

if (n > 1) n = 0;

The word if is a keyword. You cannot use it in any other way or for any other purpose.

In REBOL, words are symbols defined within dynamically created contexts. There are no restrictions on what words are used or how they are used.

For example, the word if is used several ways here:

if n > 1 [n: 0]

words: [if while loop until]

help if

obj: object [
    if: 12
    fi: 21
    print if
]

So, the context defines the word's value and its usage.

Formats

There are a few different formats for words, depending on their intended usage:

Notation Meaning
word Get the natural value of the word. (If the value is a function, evaluate it, otherwise return it.)
word: Sets the word (like assignment) to a value.
:word Gets the word's value without evaluating it. (Useful for getting the value of a function.)
'word Treat word as a value (a word symbol). Does not evaluate it.
/word Treat the word as a refinement. Used mainly for optional arguments.

Related functions

Action Type Word Type Test Conversion
word:
set-word!
set-word?
to-set-word
:word
get-word!
get-word?
to-get-word
word
word!
word?
to-word
'word
lit-word!
lit-word?
to-lit-word

Format

Words are composed of alphabetic characters, numbers, and any of the following characters:

? ! . ' + - * & | = _ &#126;

A word cannot begin with a number, and there are also some restrictions on words that could be interpreted as numbers. For instance, -1 and +1 are numbers, not words.

The end of a word is marked by a space, a newline, or one of the following characters:

[ ] ( ) { } " : ; /

Thus, the square brackets of a block are not part of a word:

[test]

The following characters are not allowed in words:

@ # $ % ^ ,

Words can be of any length, but cannot extend past the end of a line.

this-is-a-very-long-word-used-as-an-example

Sample words are:

Copy print test

number?  time?  date!

image-files  l'image

++ -- == +-

***** *new-line*

left&right left|right

The REBOL language is not case-sensitive. The words following words:

blue

Blue

BLUE

all refer to the same word. The case of the word is preserved when it is printed.

Words can be reused. The meaning of a word is dependent on its context, so words can be reused in different contexts. You can reuse any word, even predefined REBOL words. For instance, the REBOL word if can be used in your code differently than how it is used by the REBOL interpreter.

Creation

The to-word function converts values to the word! datatype.

probe to-word "test"
test

The to-set-word function converts values to the set-word! datatype.

probe make set-word! "test"
test:

The to-get-word function converts values to the get-word! datatype.

probe to-get-word "test"
:test

The to-lit-word function converts values to the lit-word! datatype.

probe to-lit-word "test"
'test

Related

Use word?, set-word?, get-word?, and lit-word? to test the datatype.

probe word? second [1 two "3"]
true
if set-word? first [word: 10] [print "it is set"]
it is set
probe get-word? second [pr: :print]
true
probe lit-word? first ['foo bar]
true


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