4Data Type
4.1Overview
A value has a corresponding Data Type that defines its behavior and properties.
Each Data Type is bound with a type name, which usually appears in argument list of function call.
Name spaces for Data Type are completely isolated from those for variable and function names.
As each Data Type has a one-to-one relationship with a corresponding Class, those terms have almost the same meaning within documents in many cases.
Data types are categorized into two types: Primitive Data Type and Object Data Type.
A value of Primitive Data Type holds its content in as small memory as possible. It doesn't include any Environment in it and doesn't have any methods with side effects. Among them are nil, boolean, complex, number, rational, string and symbol types.
A value of Object Data Type owns Object data that is a sort of Environment, which allows operations with side effects. Most Data Types except for what are picked up as Primitive Data Types above belong to this.
4.2Primitive Data Types
Below is a list of Primitve Data Types, which also shows one of the typical ways to instantiate values of each type.
nilA value of
niltype is used to indicate an invalid result or status. It is often used as a returned value of a function when it fails its expected work. A variablenilhas a value of nil type.nilSince
nilis the only instance ofniltype, the termnilcan both mean the name of the value and its type.booleanValues of
booleantype are used to determine whether something is in a true or a false state. Variables namedtrueandfalseare assigned with a true value and a false value respectively.true falseIn a function like
ifhaving arguments to check true/false condition and in a logical calculation,falseandnilonly are determined as a false state while other values are treated as a true state. Note that a zero value ofnumbertype is recognized as a true, not a false.complexA number literal suffixed by
jinstantiates a value ofcomplextype that represents a complex number.3.14j 1000j 1e3jSee chapter Mathematic Functions for more detail.
numberA number literal without any suffix instantiates a value of
numbertype.3.14 1000 1e3 0xaabbrationalA number literal suffixed by
rinstantiates a value ofrationaltype that represents a rational number.3r 123rSee chapter Mathematic Functions for more detail.
stringA string literal without any suffix instantiates a value of
stringtype.'hello world' R''' message text '''symbolAn identifier preceded by a back quote instantiates a value of
symboltype.`foo `bar
4.3Object Data Types Frequently Used
4.3.1List
If one or more elements are surrounced by a pair of square brackets, it would instantiate a value of list type. Any type of value can be an element of lists.
[3, 1, 4, 1, 5, 9]
['hello', 'world', 3, 4, 5]
4.3.2Iterator
If one or more elements are surrounced by a pair of parentheses, it would instantiate a value of iterator type. Any type of value can be an element of iterators.
(3, 1, 4, 1, 5, 9)
('hello', 'world', 3, 4, 5)
To create an iterator that contains only one element, be sure to put a comma afther the element like following:
(3,)
An expression (3) is recognized as an ordinary value of number 3.
Operator .. creates an iterator that generates a sequence of numbers. An expression x..y creates an iterator that generates a sequence starting from x and being increased by one until y.
1..10
An expression x.. creates an iterator that generates a sequence starting from x and being increased by one indefinitely.
1..
Lists and iterators are convertible to each other. For instance, a list can be converted to an iterator by using list#each method like following.
[3, 1, 4, 1, 5, 9].each()
An iterator can be converted to an list by surrounding it with square brackets.
[1..10]
In many cases, an iterator is generated as a value returned from a function, which represents a series of multiple results. The most commonly used function may be readlines, which creates an iterator that reads a stream and returns strings splitted by line.
4.3.3Dictionary
dict is a dictionary that contains key-value pairs as its elements where a key is one of number, string or symbol and a value is of any type.
You can create a dictionary by surrounding key-value pairs by %{ and }.
There are several ways to describe the pairs. The most recommended way is to use => operator between each key and value like following.
%{
`symbol1 => 'value 1'
`symbol2 => 'value 2'
`symbol3 => 'value 3'
}
A pair can also be described as a list containing a key and a value.
%{
[`symbol1, 'value 1']
[`symbol2, 'value 2']
[`symbol3, 'value 3']
}
You can also describe keys and values alternately in one-dimentional format.
%{
`symbol1, 'value 1'
`symbol2, 'value 2'
`symbol3, 'value 3'
}
4.3.4Expression
Any expression preceded by a back quote instantiates a value of expr type.
`(x + y) `func(x) `{ println('hello'), x += 1 }
4.3.5Binary
A string literal preceded by b instantiates a value of binary type.
b'\x00\x01\0x02\0x03'