REBOL Programming/Language Features/Types

Generally datatypes are sets of values and some operations that can be performed with the values. The main purpose of knowing a datatype of a value is to know the operations we can perform with the value.

REBOL has 45 basic datatypes to handle real world concepts like URLs, email addresses, telephone numbers, currency amounts. By stigmatizing each value with a value type, REBOL restricts the kinds of things that a program can do to values.

The richness facilitates the readability of REBOL scripts as well as block parsing as described elsewhere.

The datatype safety means that REBOL protects us from mixing different types which might cause unexpected errors. However, we can specify that a function be polymorphic i.e. is able to accept more than one basic datatype for its argument.

type? is an example of a polymorphic function in that it will accept an argument of any datatype and yield its datatype as a result.

>> type? http://www.rebol.com
== url!

Rebol datatypes can be divided to two groups: virtual datatypes and basic datatypes. Virtual datatypes are datatypes that serve as common "ancestors" for groups of basic datatypes.

As stated above, there is no implicit type conversion in REBOL, i.e. when we want to convert a value to a specific datatype, we have to do it explicitly and the conversion needs to be possible. This is how to convert a string to an integer:

>> to integer! "145"
== 145

This rule seems to be contradicted by working code such as:

>> 1 + 2.0
== 3.0

which must have converted the integer 1 to a decimal before adding it to the decimal 2.0. However, the arguments are not converted by the REBOL interpreter before they get to the + operator. It is the + operator which is able to accept and convert that particular combination of types (integer and decimal) before doing its work and returning the result.

Generally the to function is the first function to consider when we are doing a type conversion. The type argument of the to function may be an example value instead of a datatype: