File: datatypes.rst

package info (click to toggle)
squirrel3 3.1-8.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,380 kB
  • sloc: cpp: 12,722; ansic: 917; makefile: 316; python: 40
file content (158 lines) | stat: -rw-r--r-- 4,162 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
.. _datatypes_and_values:

=====================
Values and Data types
=====================

Squirrel is a dynamically typed language so variables do not have a type, although they
refer to a value that does have a type.
Squirrel basic types are integer, float, string, null, table, array, function, generator,
class, instance, bool, thread and userdata.

.. _userdata-index:

--------
Integer
--------

An Integer represents a 32 bits (or better) signed number.::

    local a = 123 //decimal
    local b = 0x0012 //hexadecimal
    local c = 075 //octal
    local d = 'w' //char code

--------
Float
--------

A float represents a 32 bits (or better) floating point number.::

    local a=1.0
    local b=0.234

--------
String
--------

Strings are an immutable sequence of characters to modify a string is necessary create a new one.

Squirrel's strings, behave like C or C++, are delimited by quotation marks(``"``) and can contain
escape sequences(``\t``, ``\a``, ``\b``, ``\n``, ``\r``, ``\v``, ``\f``, ``\\``, ``\"``, ``\'``, ``\0``,
``\x<hh>``, ``\u<hhhh>`` and ``\U<hhhhhhhh>``).

Verbatim string literals begin with ``@"`` and end with the matching quote.
Verbatim string literals also can extend over a line break. If they do, they
include any white space characters between the quotes: ::

    local a = "I'm a wonderful string\n"
    // has a newline at the end of the string
    local x = @"I'm a verbatim string\n"
    // the \n is copied in the string same as \\n in a regular string "I'm a verbatim string\n"

The only exception to the "no escape sequence" rule for verbatim
string literals is that you can put a double quotation mark inside a
verbatim string by doubling it: ::

    local multiline = @"
        this is a multiline string
        it will ""embed"" all the new line
        characters
    "

--------
Null
--------

The null value is a primitive value that represents the null, empty, or non-existent
reference. The type Null has exactly one value, called null.::

    local a = null

--------
Bool
--------

the bool data type can have only two. They are the literals ``true``
and ``false``. A bool value expresses the validity of a condition
(tells whether the condition is true or false).::

    local a = true;

--------
Table
--------

Tables are associative containers implemented as pairs of key/value (called a slot).::

    local t={}
    local test=
    {
        a=10
        b=function(a) { return a+1; }
    }

--------
Array
--------

Arrays are simple sequence of objects, their size is dynamic and their index starts always from 0.::

    local a  = ["I'm","an","array"]
    local b = [null]
    b[0] = a[2];

--------
Function
--------

Functions are similar to those in other C-like languages and to most programming
languages in general, however there are a few key differences (see below).

--------
Class
--------

Classes are associative containers implemented as pairs of key/value. Classes are created through
a 'class expression' or a 'class statement'. class members can be inherited from another class object
at creation time. After creation members can be added until a instance of the class is created.

--------------
Class Instance
--------------

Class instances are created by calling a *class object*. Instances, as tables, are
implemented as pair of key/value. Instances members cannot be dyncamically added or removed however
the value of the members can be changed.



---------
Generator
---------

Generators are functions that can be suspended with the statement 'yield' and resumed
later (see :ref:`Generators <generators>`).

---------
Userdata
---------

Userdata objects are blobs of memory(or pointers) defined by the host application but
stored into Squirrel variables (See :ref:`Userdata and UserPointers <embedding_userdata_and_userpointers>`).


---------
Thread
---------

Threads are objects that represents a cooperative thread of execution, also known as coroutines.

--------------
Weak Reference
--------------

Weak References are objects that point to another(non scalar) object but do not own a strong reference to it.
(See :ref:`Weak References <weak_references>`).