Skip to content

Commit 64bc383

Browse files
authored
Improve and expand the data types section in the main tutorial (#122)
* change: improve and expand the data types section in the main tutorial * patch: fix typo in header in compound data type section * patch: adjust ranges to show just the guaranteed amount and added a disclaimer to the top. * patch: add range to missed section * patch: adding pluses to data type byte size to signify minimum and expansibility
1 parent 1dfea00 commit 64bc383

File tree

1 file changed

+99
-21
lines changed

1 file changed

+99
-21
lines changed

docs/tutorials/programming-language/main/02-00-basics/02-04-data-types.md

Lines changed: 99 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,97 @@ convention for constants is `ALL_UPPER_CASE`.
1111

1212
## 2.4.1. Value Types
1313

14-
Vala supports a set of the simple types as most other languages do.
15-
16-
- Byte, `char`, `uchar`; their names are *char* for historical
17-
reasons.
18-
- Character, `unichar`; a 32-bit Unicode character
19-
- Integer, `int`, `uint`
20-
- Long Integer, `long`, `ulong`
21-
- Short Integer, `short`, `ushort`
22-
- Guaranteed-size Integer, `int8`, `int16`, `int32`, `int64` as well
23-
as their unsigned siblings `uint8`, `uint16`, `uint32`, `uint64`.
24-
The numbers indicate the lengths in bits.
25-
- Float number, `float`, `double`
26-
- Boolean, `bool`; possible values are `true` and `false`
27-
- Compound, `struct`
28-
- Enumeration, `enum`; represented by integer values, not as classes
29-
like Java's enums
30-
31-
Here are some examples.
14+
Vala supports a set of the simple types as most other languages do.
15+
16+
The following ranges may differ based on a variety of factors, such as
17+
the platform, the compiler, the CPU architecture, etc.
18+
19+
### 2.4.1.1 Byte
20+
21+
| Type | Size | Range |
22+
|---------|----------|----------------------|
23+
| `char` | 1+ bytes | at least -127 to 127 |
24+
| `uchar` | 1+ bytes | 0 to at least 255 |
25+
26+
These have the name `char` in them for historical reasons.
27+
28+
### 2.4.1.2 Integers
29+
30+
| Type | Size | Range |
31+
|--------|----------|------------------------------------------|
32+
| `int` | 4+ bytes | at least -2,147,483,647 to 2,147,483,647 |
33+
| `uint` | 4+ bytes | 0 to at least 4,294,967,295 |
34+
35+
### 2.4.1.3 Long
36+
37+
| Type | Size | Range |
38+
|---------|----------|------------------------------------------|
39+
| `long` | 4+ bytes | at least -2,147,483,647 to 2,147,483,647 |
40+
| `ulong` | 4+ bytes | 0 to at least 4,294,967,295 |
41+
42+
### 2.4.1.4 Short
43+
44+
| Type | Size | Range |
45+
|----------|----------|----------------------------|
46+
| `short` | 2+ bytes | at least -32,767 to 32,767 |
47+
| `ushort` | 2+ bytes | 0 to at least 65,535 |
48+
49+
### 2.4.1.5 Guaranteed-size Signed Integers
50+
51+
| Type | Size | Range |
52+
|---------|----------|------------------------------------------------------------------|
53+
| `int8` | 1+ byte | at least -127 to 127 |
54+
| `int16` | 2+ bytes | at least -32,767 to 32,767 |
55+
| `int32` | 4+ bytes | at least -2,147,483,647 to 2,147,483,647 |
56+
| `int64` | 8+ bytes | at least -9,223,372,036,854,775,807 to 9,223,372,036,854,775,807 |
57+
58+
### 2.4.1.6 Guaranteed-size Unsigned Integers
59+
60+
| Type | Size | Range |
61+
|----------|----------|------------------------------------------|
62+
| `uint8` | 1+ byte | 0 to at least 255 |
63+
| `uint16` | 2+ bytes | 0 to at least 65,535 |
64+
| `uint32` | 4+ bytes | 0 to at least 4,294,967,295 |
65+
| `uint64` | 8+ bytes | 0 to at least 18,446,744,073,709,551,615 |
66+
67+
### 2.4.1.7 Floating Points
68+
69+
| Type | Size | Range | Precision |
70+
|----------|---------|------------------------|------------|
71+
| `float` | 4 bytes | 1.18e-38 to 3.4e+38 | ~7 digits |
72+
| `double` | 8 bytes | 2.23e-308 to 1.80e+308 | ~16 digits |
73+
74+
Based on the IEEE 754-1985 standard.
75+
76+
### 2.4.1.8 Array Indexing / Loop Counting
77+
78+
| Type | Size | Range |
79+
|-----------|--------------|------------------------------------------------------------------|
80+
| `size_t` | 2 to 8 bytes | 0 to at least 18,446,744,073,709,551,615 |
81+
| `ssize_t` | 2 to 8 bytes | at least -9,223,372,036,854,775,807 to 9,223,372,036,854,775,807 |
82+
83+
For more information, please refer to [size_t](https://en.cppreference.com/w/c/types/size_t.html) on the C++ reference.
84+
85+
### 2.4.1.9 Booleans
86+
87+
| Type | Size | Values |
88+
|--------|-------|---------------|
89+
| `bool` | 1 bit | true or false |
90+
91+
### 2.4.1.10 Unicode Characters
92+
93+
| Type | Size | Standard |
94+
|------------|--------------|----------|
95+
| `unichar` | 4 bytes | UTF-32 |
96+
| `unichar2` | 1 to 2 bytes | UTF-16 |
97+
98+
### 2.4.1.11 Compound
99+
- `struct`
100+
101+
### 2.4.1.12 Enumeration
102+
- `enum` : values are integers, unlike Java's enums
103+
104+
### Examples
32105

33106
```vala
34107
unichar c = 'u';
@@ -63,8 +136,13 @@ with *.MIN* and *.MAX*, e.g. `int.MIN` and `int.MAX`.
63136

64137
## 2.4.2. Strings
65138

66-
The data type for strings is `string`. Vala strings are UTF-8 encoded
67-
and immutable.
139+
| Type | Size per Character | Max Length (theoretical) |
140+
|---------------------|--------------------|------------------------------|
141+
| `string` (UTF-8) | 1 byte | 0 - 2,147,483,647 characters |
142+
| `string16` (UTF-16) | 1 to 2 bytes | 0 - 2,147,483,647 characters |
143+
| `string32` (UTF-32) | 4 bytes | 0 - 2,147,483,647 characters |
144+
145+
Strings in Vala are immutable.
68146

69147
```vala
70148
string text = "A string literal";
@@ -385,7 +463,7 @@ Here are some examples:
385463
var a = 123; // int
386464
var b = 123u; // uint
387465
var c = 123l; // long
388-
var d = 123ll; // ulong
466+
var d = 123ll; // int64
389467
var e = 123ul; // ulong
390468
var f = 123ull; // uint64
391469
```

0 commit comments

Comments
 (0)