Datatypes


A Red program is a sequence of words. During evaluation, Red classifies every word as a specific datatype and acts accordingly. Everything in Red has a datatype, even datatypes. By convention, datatypes have an exclamation mark at the end of their names.
Truth is, there are more datatypes than most people care about, as you can see here, so I will separate this chapter in three parts: first I will list of the most relevant datatypes from the point of view of a beginner, then a few datatypes that can help you in the near future, and finally I will just mention the rest of them.

Note: Although it is true that Series and Blocks are datatypes, they are so important they have their own chapter.

To get a list of all datatypes, type:

>> ? datatype!

To learn now To learn later
none! vector!
logic! hash!
string! map!
char! issue!
integer! url!
float! email!
file! image!
path!
time!
date!
point! and pair!
percent!
tuple!
Datatype classes - number! & scalar!

1 - Datatypes you should learn now:

none!

The equivalent of "null" in other programming languages. A non-existing data.

>> a: [1 2 3 4 5] 
== [1 2 3 4 5]
>> pick a 7
== none

logic!

Aside from the classic true and false, Red recognizes on , off, yes and no as logic! datatype.

>> a: 2 b: 3
== 3
>> a > b
== false
>> a: on
== true
>> a
== true
>> a: off
== false
>> a
== false
>> a: yes
== true
>> a
== true
>> a: no
== false
>> a
== false

string!

a series of chars within quotes curly brackets {}. If your string spans over more than one line, curly brackets are mandatory

>> a: "my string"
== "my string"
>> a: {my string}
== "my string"
>> a: {my
{    string}
== "my^/string"
>> print a
my
string
>> a: "my new                         ;trying to span over more than one line
*** Syntax Error: invalid value at {"my new}

char!

Preceded by # and within quotes, char! values represent a Unicode code point. They are integer numbers in the range hexadecimal 00 to hexadecimal 10FFFF. (0 to 1,114,111 in decimal.)

#"A" is a char!

"A" is a string!

It may undergo math operations.

>> a: "my string"
== "my string"
>> pick a 2
== #"y"
>> poke a 3 #"X"
== #"X"
>> a
== "myXstring"
>> a: #"b"
== #"b"
>> a: a + 1
== #"c"

integer!

32 bit whole signed numbers. From −2,147,483,648 to 2,147,483,647. If a number is outside this range, Red assigns it a float! datatype.

Note: Dividing 2 integers gives a truncated result:

>> 7 / 2
== 3

float!

64 bit floating point numbers. Represented by numbers with a period or using the e-notation.

>> 7.0 / 2
== 3.5
>> 3e2
== 300.0
>> 6.0 / 7
== 0.8571428571428571

file!

Preceded by %. If you are not using the current path, you should add the path within quotes. The path uses forward slashes (/), and back slashes (Windows format) are converted automatically.

>> write %myfirstfile.txt "This is my first file"
>> write %"C:\Users\André\Documents\RED\mysecondfile.txt" "This is my second file"
>>

path!

Used to access items inside larger structures using "/". Can be used in many different situations, for example:

>> a: [23 45 89]
== [23 45 89]
>> print a/2
45

Slashes "/" are also used to access objects an refinements. I don't know the inner workings of the Red interpreter, but it seems to me that those are cases of the path! type.

time!

Time is expressed as hours:minutes:seconds.subseconds. Notice that seconds and subseconds are separated by a period, not a colon. You can access each one with a refinement.

>> mymoment: 8:59:33.4
== 8:59:33.4
>> mymoment/minute: mymoment/minute + 1
== 60
>> mymoment
== 9:00:33.4

date!

Red accepts dates in a variety of formats:

>> print 31-10-2017
31-Oct-2017
>> print 31/10/2017
31-Oct-2017
>> print 2017-10-31
31-Oct-2017
>> print 31/Oct/2017
31-Oct-2017
>> print 31-october-2017
31-Oct-2017
>> print 31/oct/2017
31-Oct-2017
>> print 31/oct/17   ;only works if the year is the last field, but be careful: 1917 or 2017?.
31-Oct-2017

Red also checks if dates are valid, even considering leap years.
You can refer to day, month or year using refinements:

>> a: 31-oct-2017
== 31-Oct-2017
>> print a/day
31
>> print a/month
10
>> print a/year
2017

point! and pair!

Point! and pair! seem to me as being the same. Probably pair! exists to maintain Rebol compatibility.
Represents points in a cartesian coordinate system (x y axys). Represented by integers separated by "x" e.g. 23x45.

>> a: 12x23
== 12x23
>> a: 2 * a
== 24x46
>> print a/x  
24
>> print a/y
46

percent!

Represented by adding the "%" symbol after the number.

>> a: 100 * 11.2%
== 11.2
>> a: 1000 * 11.3%
== 113.0

tuple!

A tuple! is a list of 3 up to 12 bytes (bytes range from 0 to 255) separated by periods. Notice that 2 numbers separated by a period is a float! not a tuple!
Tuples are useful to represent things like version numbers, IP addresses , and colours (example: 0.255.0).
A tuple! is not a series, so most series operations give an error when applied. Some operations that can be performed on a tuple! are: random, add, divide, multiply, remainder, subtract, and, or, xor, length?, pick (not poke), reverse.

>> a: 1.2.3.4
== 1.2.3.4
>> a: 2 * a
== 2.4.6.8
>> print pick a 3
6
>> a/3: random 255
== 41
>> a
== 2.4.41.8

Datatype classes - number! and scalar!

Some datatypes are classes of datatypes:

Any of the following datatypes is also a number! datatype: integer!, float!, percent!

And any any of the following datatypes is also a scalar! datatype: char!, integer!, float!, pair!, percent!, tuple!, time!, date!


2 - Datatypes that may help you in the near future:

vector!

Vectors are high performance series of integer! ,float!, char! or percent!

To create a vector you must use make vector!

>> a: make vector! [33 44 52]
== make vector! [33 44 52]
>> print a
33 44 52
>> print a * 8
264 352 416

hash!

Series with high performance lookups. May save some time when dealing with large data sets.

>> a: make hash! [a 33 b 44 c 52]
== make hash! [a 33 b 44 c 52]
>> select a [c]
== 52
>> select a 'c
== 52

map!

Maps are high performance dictionaries that associate keys with values (key1: val1 key2: val2 ... key3: val3).

To set and retrieve values from the dictionary we use select (from series) and a a special action: put.

>> a: make map! ["mini" 33 "winny" 44 "mo" 55]
== #(
    "mini" 33
    "winny" 44
    "mo" 55
...
>> print a
"mini" 33
"winny" 44
"mo" 55
>> print select a "winny"
44
>> put a "winny" 99
== 99
>> print a
"mini" 33
"winny" 99
"mo" 55

issue!

Series of characters used to sequence symbols or identifiers for things like telephone numbers, model numbers, serial numbers, and credit card numbers. An issue! has to start with the character "#". Most characters can be used inside an issue!, a notable exception being the slash "/".

>> a: #333-444-555-999
== #333-444-555-999
>> a: #34-Ab.77-14
== #34-Ab.77-14

url!

Represented by <protocol>://<path>

>> a: read http://www.red-lang.org/p/about.html
== {<!DOCTYPE html>^/<html class='v2' dir='ltr' x
>>

email!

Used to identify email addresses. No detailed syntax-checking is performed, it must only contain an @ character.

>> a: [email protected]
== [email protected]
>> type? a
== email!

image!

To create a image! you must use make image!
The external image formats supported are GIF, JPEG, PNG and BMP.
When you load an image file, the data is typed as image! It is unlikely that you will create image with text, but the format would be:

>> a: make image! [30x40 #{ ; here goes the data...
You can change or get information from your image using the actions that apply to series:
>>  a: load %heart.bmp
== make image! [30x20 #{
    00A2E800A2E800A2E800A
>> print a/size
30x20
>> print pick a 1  ; getting the RGBA data of pixel 1
0.162.232.0
>> poke a 1 255.255.255.0  ; changing the RGBA data of pixel 1
== 255.255.255.0
>>

3 - Datatypes that you probably won't have to deal with so soon:

If you do, look at red-by-example.org.

function!

object!

handle!

event!

unset!

block!

Any series within brackets.

paren!

Any series within parentheses.

set-word! lit-word! get-word!

w: - sets the word to a value. Its type is set-word!

:w - gets the word value. No evaluation. Its type is get-word!

'w - treats the word as a symbol, with no evaluation. Its type is lit-word (i.e. literal word)

refinement!

Preceded by "/" - indicate a variation in the use or an extension of the meaning of a function!, object!, file! or path!.

native!

action!

Is the datatype of all "actions" in red, e.g. add , take , append, negate etc.

red>> action? :take     ; Colon is mandatory.
== true

To get a list of all action! words type:

>> ? action!

op!

Is the datatype of infix operators , like + or **.

lit-path!

set-path!

get-path!

routine!

Used to link to external code

bitset!

typeset!

error!

binary!

tag!

word!

The mother of all datatypes. When a word is created it has this datatype. Described in Notes on syntax chapter.

datatype!

Is the datatype of all the datatype! words listed in this chapter.

results matching ""

    No results matching ""