Skip to content

Commit

Permalink
improve docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Jul 26, 2023
1 parent b50e159 commit 1710199
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/common-concepts/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ fn main() {

---

Anonymous functions can access the definitions of the block in which they are defined.
Anonymous functions can access the definitions of the block in which they are defined. But doesn't referring them, copies all definition for itself. Therefore, you can't affect to parent scope definitions in most case.

For example:
```
Expand Down
2 changes: 1 addition & 1 deletion src/common-concepts/structures.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Just give structure identifier as receiver. Not generics or type alias.
:::

### Receiver Parameters
Receivers indicate how instance the function will use. Receiver parameters must be the first parameter of each method.
Receivers indicate how instance the function will use. Receiver parameters must be the first parameter of each method. Receiver parameters are also a [reference](/memory/references) by default.

There are two types of receiver parameters;

Expand Down
6 changes: 6 additions & 0 deletions src/memory/references.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ fn main() {
}
```

### Anonymous Functions with References

Anonymous functions copies instead of referencing the definitions of the scope in which they are defined, for safety reasons. Thus, a possible danger of dangling is prevented. But some copied things can be undsgr, one of them being references. Even if the references are copied, they will still continue to point to the same address as it is an address alias in nature. Therefore, there is a danger of dangling the reference if it goes out of scope. To avoid this, Safe Jule does not allow you to use references from parent scopes.

If you're sure it's safe to do so, [Unsafe Jule](/unsafe-jule/) lets you access such dangerous references.

## Reference Parameters

Reference parameters must take an lvalue as an argument. To specify a reference parameter, the parameter identifier must be preceded by the `&` operator.
Expand Down
16 changes: 16 additions & 0 deletions src/unsafe-jule/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Benefits of Unsafe Jule:
- Call unsafe functions or methods
- Concurrent calls with reference parameters
- Pass pointer to reference
- Access reference from parent scope

Note that this does not lead to a completely unsafe use of Jule. Other than the listed unsafe behaviors, Safe Jule will continue to show itself. This means you get a level of safety even with unsafe blocks.

Expand Down Expand Up @@ -126,3 +127,18 @@ For example:
```
my_function(unsafe { *my_pointer })
```

## Access Reference from Parent Scope

Anonymous functions copy the definitions of the scope in which they are defined for safety reasons, they do not refer to them. But a copied reference is still a reference and is in danger of dangling. Therefore, anonymous functions do not use references from parent scopes. But Unsafe Jule lets you do just that. Access the relevant reference only with Unsafe Jule.

For example:
```
fn main() {
let x = 10
let &y = x
fn() {
unsafe { outln(y) }
}()
}
```

0 comments on commit 1710199

Please sign in to comment.