Skip to content

Commit

Permalink
update variables
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Jan 7, 2025
1 parent 1a8ef52 commit de7e594
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/common-concepts/variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ For example:
```jule
let age = 18
```
The data type of the above variable is defaulted to `int`.
The data type of the above variable is defaulted to `int`.

## Type Annotation
You may want to annotate the type, it is possible. It also brings you advantages.
Expand Down Expand Up @@ -99,6 +99,31 @@ fn main() {
Binded globals can be declare via `let` keyword.
:::

### Initialization
Global variables are initialized during the earliest stages of the program. At the time of their initialization, no `init` calls have been executed yet.

If the execution of an `init` function is necessary for the proper initialization of global variables, care must be taken. Otherwise, unexpected results may occur.

For example:
```jule
static foo = bar()
static mut baz = 0
fn bar(): int {
ret baz
}
fn init() {
baz = 123
}
fn main() {
println(foo)
println(bar())
}
```
In the example code above, the `foo` variable is initialized with the value returned by the `bar()` function call. Since the `baz` variable has not yet been assigned a value by the `init` function, the `foo` variable is initialized with `0`. As a result, the program's output will be `0` and `123`.

## Constant Variables

See relevant [comptime documentations](/comptime/comptime-evaluation) for information about constant variables.
Expand Down

0 comments on commit de7e594

Please sign in to comment.