Skip to content

Commit cf4760e

Browse files
committed
add references
1 parent c42eacc commit cf4760e

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

.vitepress/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ export default defineConfig({
103103
items: [
104104
{ text: 'Immutability', link: '/memory/immutability' },
105105
{ text: 'Pointers', link: '/memory/pointers' },
106+
{ text: 'References', linkd: '/memory/references' },
106107
{ text: 'Memory Management', link: '/memory/memory-management' },
107108
],
108109
},

src/memory/memory-management.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Jule does the memory management itself. But it's not fully automatic. You decide
33

44
Jule uses reference counting for heap allocations. It is automatically released when the reference count of the pointer reaches zero, that is, when it is certain that the heap allocation is no longer used. It is guaranteed that no allocation goes unnoticed and is also not released while the allocation is still in use.
55

6-
## References
6+
## Reference Types
77
References are heap allocation data types. A reference is annotated by an `&` operator.
88

99
A reference is always heap-allocation and is always within the reference counting. When a pointer to a reference is taken, you don't get a pointer to the reference. You get a pointer to the address of the heap allocation that the reference is using.
@@ -68,7 +68,7 @@ fn main() {
6868
```
6969

7070
### Allocation Management
71-
References can be `nil` (aka null). This is safe, when a `nil` reference is used unsafe it will give you a panic that it is nil. When a reference is set to nil, the reference count continues to run. So when you assign to nil any reference, this reference countdown by reference count and deallocates if necessary.
71+
References can be `nil` (aka null). This is safe, when a `nil` reference is used unsafe it will give you a panic that it is nil. When a reference is set to nil, the reference count continues to run. So when you assign to nil any reference, this reference countdown by reference count and deallocates if necessary.
7272

7373
Classic assignment cannot be made to assign a reference to nil. Classic assignments are always assignments to the data carried by the reference. If the data type carried by the reference is nil compatible, the nil assignment is made to the data it contains.
7474

src/memory/references.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# References
2+
3+
References can be confused with the reference types described in memory management, but they are completely different things. References are like an alias for an lvalue. You can think of them as pointers but they are safer.
4+
5+
They are used with `&` operator in syntax. You can't have nested references, for example you can have a pointer pointing to a pointer but not a reference referencing to a reference.
6+
7+
References point to the value from which they were initialized and must receive an initialize expression. The lvalue pointing to later references cannot be changed. Any assignment statement is always about changing the data they refer to.
8+
9+
## Reference Variables
10+
11+
Reference variables are variables that reference an lvalue. Any assignment made affects the referenced lvalue. Declared with `&` operator.
12+
13+
For example:
14+
```
15+
fn main() {
16+
let mut a = 20
17+
let mut &b = a
18+
b += 20
19+
outln(a) // 40
20+
}
21+
```
22+
23+
Also you can use reference types in mult-declarative assignments.
24+
25+
For example:
26+
```
27+
fn main() {
28+
let mut a = 20
29+
let (mut &x, mut y) = a, 20
30+
x += y
31+
outln(a) // 40
32+
}
33+
```
34+
35+
## Reference Parameters
36+
37+
Reference parameters must take an lvalue as an argument. To specify a reference parameter, the parameter identifier must be preceded by the `&` operator.
38+
39+
For example:
40+
```
41+
fn add_20(mut &a: int) {
42+
a += 20
43+
}
44+
45+
fn main() {
46+
let mut a = 20
47+
add_20(a)
48+
outln(a) // 40
49+
}
50+
```

0 commit comments

Comments
 (0)