|
@@ -0,0 +1,393 @@
|
|
1
|
+# Petal's type system
|
|
2
|
+
|
|
3
|
+A type system must be unsound (it accepts incorrect programs), incomplete (it rejects correct
|
|
4
|
+programs), or undecidable (some programs can't be analyzed). Petal would like a sound type system
|
|
5
|
+and accepts being incomplete.
|
|
6
|
+
|
|
7
|
+The type system operates at compile-time insofar as possible. If something could just as easily be a
|
|
8
|
+compile-time error as a runtime error, it should be a compile-time error.
|
|
9
|
+
|
|
10
|
+## Use of uninitialized variables
|
|
11
|
+Using uninitialized variables is not allowed.
|
|
12
|
+
|
|
13
|
+## Special types
|
|
14
|
+Petal has a few special types that are a bit abstract:
|
|
15
|
+* `never`
|
|
16
|
+* `unit`
|
|
17
|
+* `raw`
|
|
18
|
+* `any`
|
|
19
|
+
|
|
20
|
+### never
|
|
21
|
+`never` is a type that has no values. The main effect of this is that a function declared as
|
|
22
|
+returning `never` can't return a value. It must instead throw an exception, halt the program,
|
|
23
|
+something like that. This is useful for `panic`-type functions that log an error and then abort the
|
|
24
|
+program, or functions that throw an exception in every code path, or possibly for functions that
|
|
25
|
+loop infinitely.
|
|
26
|
+
|
|
27
|
+Other than that, you can use this like a normal type. You can't ever initialize a variable with a
|
|
28
|
+value of type `never`, and you can't use uninitialized variables, so this doesn't blow up in your
|
|
29
|
+face. This property propagates to any type that has a field of type `never`. The following struct
|
|
30
|
+can't be built, for instance:
|
|
31
|
+
|
|
32
|
+```
|
|
33
|
+struct HasNever
|
|
34
|
+{
|
|
35
|
+ never a
|
|
36
|
+}
|
|
37
|
+```
|
|
38
|
+
|
|
39
|
+`never` is not a bottom type from type theory. It does not implicitly convert to anything, and
|
|
40
|
+nothing implicitly converts to it. It is an empty type, and it's sometimes called the diverging
|
|
41
|
+type.
|
|
42
|
+
|
|
43
|
+The `never` type has size 0.
|
|
44
|
+
|
|
45
|
+### unit
|
|
46
|
+`unit` is a type that only has one value named `unit()`.
|
|
47
|
+
|
|
48
|
+Every function returns a value. A function that would be declared returning `void` in Java or C
|
|
49
|
+instead returns `unit`.
|
|
50
|
+
|
|
51
|
+The `unit` type has size 0.
|
|
52
|
+
|
|
53
|
+### raw
|
|
54
|
+`raw` refers to raw memory. It's only allowed in unsafe code. Otherwise, it represents an opaque
|
|
55
|
+byte (`uint8` that supports no operations aside from equality). `raw&` is a reference to bytes that
|
|
56
|
+could represent anything; `raw[]` is some memory that might contain anything.
|
|
57
|
+
|
|
58
|
+### any
|
|
59
|
+`any` is anything. Any object, any struct, anything. It's the root of the type hierarchy.
|
|
60
|
+
|
|
61
|
+## Builtin types
|
|
62
|
+### Numeric types
|
|
63
|
+* `int`
|
|
64
|
+* `int8`
|
|
65
|
+* `uint8`
|
|
66
|
+* `int16`
|
|
67
|
+* `uint16`
|
|
68
|
+* `int32`
|
|
69
|
+* `uint32`
|
|
70
|
+* `int64`
|
|
71
|
+* `uint64`
|
|
72
|
+* `int128`
|
|
73
|
+* `uint128`
|
|
74
|
+* `float32`
|
|
75
|
+* `float64`
|
|
76
|
+
|
|
77
|
+The trailing number is the number of bits in the type. The prefix indicates the general type: signed
|
|
78
|
+integer `intX`, unsigned integer `uintX`, IEEE floating point `floatX`.
|
|
79
|
+
|
|
80
|
+`int` is an alias for `int64`. While most numbers are small, a 32-bit integer is a little small for
|
|
81
|
+a number of real-world contexts like file length.
|
|
82
|
+
|
|
83
|
+### Strings and characters
|
|
84
|
+Petal supports UTF-8 / UTF-16 / UTF-32 code units:
|
|
85
|
+* `char8`
|
|
86
|
+* `char16`
|
|
87
|
+* `char32`
|
|
88
|
+
|
|
89
|
+These are low-level tools for low-level code. `char8` is 8 bits; `char16` is 16 bits; `char32` is 32
|
|
90
|
+bits.
|
|
91
|
+
|
|
92
|
+On the higher-level side, it has:
|
|
93
|
+* `string`
|
|
94
|
+* `char`
|
|
95
|
+
|
|
96
|
+`string` is a UTF-8 encoded string. `char` represents a **grapheme cluster**. A grapheme cluster
|
|
97
|
+might be a single code unit, like `a` (U+0061); it might be a single character with multiple code
|
|
98
|
+units, like `☃` (U+2603); or it might be a character with combining marks, like `é̄` (U+0065 U+0302
|
|
99
|
+U+0304).
|
|
100
|
+
|
|
101
|
+Internally, `string` is stored as `char8[]`. To access this underlying array, use the
|
|
102
|
+`representation` property.
|
|
103
|
+
|
|
104
|
+### Boolean
|
|
105
|
+Petal supports the boolean type, `bool`. It has two possible values: `true` and `false`. It is a
|
|
106
|
+purely boolean type, not supporting arithmetic.
|
|
107
|
+
|
|
108
|
+## Derived types
|
|
109
|
+There are a number of derived types that can be constructed of other types:
|
|
110
|
+
|
|
111
|
+* tuples
|
|
112
|
+* aliases
|
|
113
|
+* functions
|
|
114
|
+* references
|
|
115
|
+* arrays
|
|
116
|
+* dicts
|
|
117
|
+
|
|
118
|
+### Aliases
|
|
119
|
+An alias is just another name for a type.
|
|
120
|
+
|
|
121
|
+In fact, an alias can be another name for anything nameable. The compiler will try to report that
|
|
122
|
+alias instead of the thing's true name when you access it through an alias.
|
|
123
|
+
|
|
124
|
+As an example:
|
|
125
|
+
|
|
126
|
+```
|
|
127
|
+alias wchar char16.
|
|
128
|
+```
|
|
129
|
+
|
|
130
|
+### Tuples
|
|
131
|
+A tuple is an ordered collection of values of varying types:
|
|
132
|
+
|
|
133
|
+```
|
|
134
|
+(int32, uint8, string) tuple = (7451, 12, "hello world").
|
|
135
|
+```
|
|
136
|
+
|
|
137
|
+Tuples are implicitly flattened:
|
|
138
|
+
|
|
139
|
+```
|
|
140
|
+alias Record (int32, string).
|
|
141
|
+alias NamedRecord (string, record).
|
|
142
|
+# Both of these options work:
|
|
143
|
+NamedRecord r1 = ("id1", (102, "value1")).
|
|
144
|
+NamedRecord r2 = ("id2", 51, "value2").
|
|
145
|
+```
|
|
146
|
+
|
|
147
|
+A single-item tuple is the same as its content:
|
|
148
|
+
|
|
149
|
+```
|
|
150
|
+alias JustAString (string).
|
|
151
|
+JustAString r3 = "hello".
|
|
152
|
+string s = r3.
|
|
153
|
+```
|
|
154
|
+
|
|
155
|
+Tuples can be indexed like arrays, but the index must be a compile-time constant:
|
|
156
|
+
|
|
157
|
+```
|
|
158
|
+NamedRecord r4 = ("id4", 12, "value3").
|
|
159
|
+println r4:1. # prints "12"
|
|
160
|
+println r4:2. # prints "value3"
|
|
161
|
+```
|
|
162
|
+
|
|
163
|
+Tuples can be implicitly expanded or created when passed to a function:
|
|
164
|
+
|
|
165
|
+```
|
|
166
|
+unit show(NamedRecord r)
|
|
167
|
+{
|
|
168
|
+ printfln "id: {} index: {} value: {}" r:0 r:1 r:2.
|
|
169
|
+}
|
|
170
|
+unit show2(string id, int32 index, string value)
|
|
171
|
+{
|
|
172
|
+ show(id, index, value).
|
|
173
|
+}
|
|
174
|
+```
|
|
175
|
+
|
|
176
|
+### Functions
|
|
177
|
+A function type holds a reference to a function. It can also include a reference to a context for
|
|
178
|
+that function.
|
|
179
|
+
|
|
180
|
+For instance:
|
|
181
|
+
|
|
182
|
+```
|
|
183
|
+int32 asciiVowels(char8[] s)
|
|
184
|
+{
|
|
185
|
+ int32 count = 0.
|
|
186
|
+ foreach c; s:byCodeUnit
|
|
187
|
+ if c in "aeiou"
|
|
188
|
+ count++.
|
|
189
|
+ return count.
|
|
190
|
+}
|
|
191
|
+func: int32 fn(string) = &asciiVowels
|
|
192
|
+```
|
|
193
|
+
|
|
194
|
+Every function returns a value. A function's arguments are effectively a tuple.
|
|
195
|
+
|
|
196
|
+### Arrays
|
|
197
|
+An array is an ordered, indexable collection of items of a given type. It offers O(1) indexing, O(n)
|
|
198
|
+search, amortized O(log n) append, and O(n) splicing and concatenation. This is also a slice type;
|
|
199
|
+two arrays may refer to the same or overlapping data.
|
|
200
|
+
|
|
201
|
+Arrays use 0-based indexing: the first item is `list[0]`, the second is `list[1]`, etc.
|
|
202
|
+
|
|
203
|
+```
|
|
204
|
+int32[] list!
|
|
205
|
+foreach v; 1..10
|
|
206
|
+ list ~= v * v.
|
|
207
|
+frontHalf = list[0:to 5].
|
|
208
|
+assert list[4] == 25.
|
|
209
|
+```
|
|
210
|
+
|
|
211
|
+Arrays can also be iterated, with or without an index:
|
|
212
|
+
|
|
213
|
+```
|
|
214
|
+int32[] list = [1, 4, 9, 16].
|
|
215
|
+foreach i, v; list
|
|
216
|
+ printfln "list[{}] = {}" i v.
|
|
217
|
+```
|
|
218
|
+
|
|
219
|
+This prints:
|
|
220
|
+
|
|
221
|
+```
|
|
222
|
+list[0] = 1
|
|
223
|
+list[1] = 4
|
|
224
|
+list[2] = 9
|
|
225
|
+list[3] = 16
|
|
226
|
+```
|
|
227
|
+
|
|
228
|
+Implementation note: arrays are implemented as:
|
|
229
|
+
|
|
230
|
+```
|
|
231
|
+struct Array
|
|
232
|
+{
|
|
233
|
+ unsafe mut T* data.
|
|
234
|
+ mut int64 length.
|
|
235
|
+ TypeInfo type.
|
|
236
|
+}
|
|
237
|
+```
|
|
238
|
+
|
|
239
|
+The `type` field may be omitted or dynamically added as appropriate.
|
|
240
|
+
|
|
241
|
+### Multidimensional arrays
|
|
242
|
+A multidimensional array is an array with multiple dimensions. Petal's multidimensional arrays are
|
|
243
|
+row-major; the first index is the row, the second is the column.
|
|
244
|
+
|
|
245
|
+A two-dimensional array is sometimes called a rectangular array. It can be used to implement a
|
|
246
|
+matrix. It looks like:
|
|
247
|
+
|
|
248
|
+```
|
|
249
|
+int32[,] rect = [
|
|
250
|
+ [11, 12, 13, 14],
|
|
251
|
+ [21, 22, 23, 24],
|
|
252
|
+ [31, 32, 33, 34],
|
|
253
|
+].
|
|
254
|
+# It's a two-dimensional array
|
|
255
|
+assert rect.lengths.length == 2.
|
|
256
|
+# The first length is 3, because the array is 3 high.
|
|
257
|
+assert rect.lengths[0] == 3.
|
|
258
|
+# The second length is 4, because the array is 4 wide.
|
|
259
|
+assert rect.lengths[1] == 4.
|
|
260
|
+
|
|
261
|
+int32 sum!
|
|
262
|
+foreach y, x, val; rect
|
|
263
|
+ sum += val.
|
|
264
|
+assert sum == 270.
|
|
265
|
+```
|
|
266
|
+
|
|
267
|
+This works identically for more than two dimensions. The compiler is guaranteed to support up to
|
|
268
|
+five dimensions.
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+### References
|
|
272
|
+A reference is an alias to an existing value.
|
|
273
|
+
|
|
274
|
+References are a boring example of the name-value distinction more interestingly demonstrated by Dr
|
|
275
|
+Charles Dodgson with a song. The song **is** _A-sitting on a Gate_, but it's called _Ways and
|
|
276
|
+Means_. The song's name is _The Aged Aged Man_, while the name is called _Haddock's Eyes_.
|
|
277
|
+
|
|
278
|
+Let's look at this example similarly:
|
|
279
|
+
|
|
280
|
+```
|
|
281
|
+int32 x = 12.
|
|
282
|
+int32& y = &x.
|
|
283
|
+println y. # 0x7FFDA7EAA968
|
|
284
|
+println $y. # 12
|
|
285
|
+$y = 15.
|
|
286
|
+println x. # 15
|
|
287
|
+x = 18.
|
|
288
|
+println $y. # 18
|
|
289
|
+```
|
|
290
|
+
|
|
291
|
+The reference type that refers to an `int32` is named `int32&`. To refer to the value that reference
|
|
292
|
+`y` points to, you use `$y`. (This is instead of `*`, which is used in a number of languages, to
|
|
293
|
+reduce ambiguity. It's obvious many readers that `println *y` should print the value that `y` points
|
|
294
|
+to, but it's a bit harder for the compiler to figure that out.)
|
|
295
|
+
|
|
296
|
+The value is 12. The value is called `x`. The address of the value is `0x7FFDA7EAA968`. And that
|
|
297
|
+address is also called `y`.
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+## Aggregate types
|
|
301
|
+An aggregate type is a type with fields and methods. A field is just a variable that values of that
|
|
302
|
+type contain. A method is a function that implicitly takes the aggregate as its first argument.
|
|
303
|
+
|
|
304
|
+### Structs
|
|
305
|
+A struct is a data type passed by value. It's a series of fields that can be accessed together, and
|
|
306
|
+it acts as a namespace for functions that deal whith those fields.
|
|
307
|
+
|
|
308
|
+A struct doesn't need to define any fields; it's valid to have a struct with no fields. This isn't
|
|
309
|
+usually useful, though.
|
|
310
|
+
|
|
311
|
+```
|
|
312
|
+struct Password
|
|
313
|
+{
|
|
314
|
+ uint8[] salt.
|
|
315
|
+ uint8[] hashed.
|
|
316
|
+ this()
|
|
317
|
+ {
|
|
318
|
+ salt = randomSalt.
|
|
319
|
+ }
|
|
320
|
+ unit set(string v) = hashed = digest v salt.
|
|
321
|
+ bool verify(string v) = hashed == digest v salt.
|
|
322
|
+}
|
|
323
|
+Password password!
|
|
324
|
+password:set "its-a-secret".
|
|
325
|
+println (toHex password:hash).
|
|
326
|
+```
|
|
327
|
+
|
|
328
|
+A password is little more than its fields bundled together.
|
|
329
|
+
|
|
330
|
+### Classes
|
|
331
|
+A class is much like a struct with more power. However, it's a little less efficient.
|
|
332
|
+
|
|
333
|
+First, the inefficiency: structs are allocated inline in their context, while each class instance is
|
|
334
|
+a separate heap allocation. Structs are accessed like other variables in the same context, but class
|
|
335
|
+instances are always accessed with references.
|
|
336
|
+
|
|
337
|
+(The compiler may, as an optimization, allocate some class instances inline if it detects it's safe
|
|
338
|
+to do so. But this isn't reliable.)
|
|
339
|
+
|
|
340
|
+Classes can participate in inheritance. They can inherit one other class; if not otherwise
|
|
341
|
+specified, they inherit `Object`. They can inherit any number of interfaces.
|
|
342
|
+
|
|
343
|
+```
|
|
344
|
+class Person
|
|
345
|
+{
|
|
346
|
+ string name.
|
|
347
|
+ this(this.name).
|
|
348
|
+ virtual unit greet()
|
|
349
|
+ {
|
|
350
|
+ printfln "Hello {}!" name.
|
|
351
|
+ }
|
|
352
|
+}
|
|
353
|
+class Employee from Person
|
|
354
|
+{
|
|
355
|
+ string id.
|
|
356
|
+ override unit greet()
|
|
357
|
+ {
|
|
358
|
+ base:greet.
|
|
359
|
+ printfln "Please remember to comply with all corporate policies, {}." id.
|
|
360
|
+ }
|
|
361
|
+ this(this.name, this.id).
|
|
362
|
+}
|
|
363
|
+let employee = Employee "Anne" "TK-421".
|
|
364
|
+employee:greet
|
|
365
|
+```
|
|
366
|
+
|
|
367
|
+Classes can be abstract. An abstract class must be marked `abstract`. An abstract class can't be
|
|
368
|
+instantiated directly but can be inherited from, and it can contain abstract methods. A non-abstract
|
|
369
|
+class that inherits from an abstract class must override all abstract methods from the base class.
|
|
370
|
+
|
|
371
|
+An abstract method may have a body but doesn't require one.
|
|
372
|
+
|
|
373
|
+### Interfaces
|
|
374
|
+An interface is like an abstract class, but it cannot have any fields and all their functions are
|
|
375
|
+implicitly abstract. It can only define function signatures. They engage in inheritance, but an
|
|
376
|
+interface can only inherit from another interface.
|
|
377
|
+
|
|
378
|
+### Concepts
|
|
379
|
+A concept is a bit like an interface. It defines a series of operations that a type must support.
|
|
380
|
+
|
|
381
|
+An aggregate may declare that it adheres to a concept.
|
|
382
|
+
|
|
383
|
+### Operator overloading
|
|
384
|
+User-defined types can overload operators. Please do not abuse this feature.
|
|
385
|
+* indexing: `index` function for retrieving a value, `setIndex` for changing a value, `range` for
|
|
386
|
+ slicing.
|
|
387
|
+* math: `add`, `subtract`, `multiply`, `divide`, `modulus`, `exponent`
|
|
388
|
+* bitwise: `bitAnd`, `bitOr`, `bitXor`
|
|
389
|
+* bitwise shifts: `rightShift`, `leftShift`, `rightRotate`, `leftRotate`
|
|
390
|
+* iteration: `range` with no arguments to return a range.
|
|
391
|
+
|
|
392
|
+## Mutability
|
|
393
|
+By default, local variables are mutable, and everything else is immutable.
|