Datatypes



It may be a good idea to take a look first at the chapters about series, as some examples use built-in functions listed there.


native! type?  Red-by-example


Returns the datatype of a value or the datatype of what is assigned to a word in the dictionary:


>> type? 33
== integer!

>> type? "house"
== string!

>> birthday: 30/07/1963
== 30-Jul-1963
>> type? birthday
== date!


Basic Datatypes:


 none!   Red documentation  Red-by-example


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!   Red documentation  Red-by-example

Aside from the classic true and false, Red recognizes on , offyes 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

Notice that, as far as I know, everything that is not false , off or no is considered true:

>> if "house" [print "It's true!"]
It's true!

>> if 0 [print "It's true!"]
It's true!

>> if [] [print "It's true!"]
It's true!

>> if [false] [print "It's true!"]  ;bizarre!
It's true!

 string!   Red documentation  Red-by-example

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

Strings are series, and can be manipulated using the the commands described in the chapters about them.

>> a: "my string"
== "my string"

>> a: {my string}
== "my string"

>> a: {my
{ string}        ;the first "{" is not a typo, is how the console shows it. Try!
== "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!  Red documentation  Red-by-example

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!  Red documentation  Red-by-example

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!   Red documentation  Red-by-example

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!   Red documentation  Red-by-example

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!   Red documentation  Red-by-example

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 and 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!   Red documentation  Red-by-example

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. Check the chapter about Time and timing.

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

>> a: now/time/precise        ; a datatype is time!
== 22:05:46.805                
>> type? a
== time!
>> a/hour
== 22
>> a/minute
== 5
>> a/second
== 46.805                        ;second is a float!

 date!   Red documentation  Red-by-example

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

  pair!   Red documentation  Red-by-example
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!   Red documentation  Red-by-example

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

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

 tuple!   Red documentation  Red-by-example

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


Words datatypes:


When you use type? to determine the datatype of a word, you usually get the datatype of the value assigned to that word, as in:


>> test: 33.8
== 33.8
>> type? test
== float!

However, the word itself (in this case "test") may assume different datatypes, depending on context:



datatype

word

word!

word:

set-word!

:word

get-word!

'word

lit-word!

/word

refinement!



>> to-word "test"
== test

>> make set-word! "test"
== test:

>> make get-word! "test"
== :test

>> make lit-word! "test"
== 'test

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!



< Previous topic                                                                                          Next topic >