Allocations
This commit is contained in:
@@ -111,6 +111,20 @@ pub class Container<T> {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Allocations
|
||||||
|
In classes, it's very important to initialize heap allocated fields in `constructor` using `path -> expr`, here is the most common example:
|
||||||
|
```mist
|
||||||
|
pub class Dog : Animal {
|
||||||
|
pub heap_alloc Box<i32>;
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
super -> Super::new();
|
||||||
|
self.heap_alloc -> Box::new(69);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
This outputs `std::ptr::write` which is a force write to memory without dropping what "exists", and it's commonly used in uninitialized memory, such as uninitialized dynamic fields.
|
||||||
|
|
||||||
## Key Characteristics
|
## Key Characteristics
|
||||||
|
|
||||||
- **Unified Scope**: Data and behavior live in one class block.
|
- **Unified Scope**: Data and behavior live in one class block.
|
||||||
|
|||||||
@@ -40,6 +40,17 @@ pub struct Inspector<'a> {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Allocations
|
||||||
|
In classes, it's very important to initialize heap allocated fields in `constructor` using `path -> expr`, here is the most common example:
|
||||||
|
```mist
|
||||||
|
pub class Dog : Animal {
|
||||||
|
constructor() {
|
||||||
|
super -> Super::new();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
This outputs `std::ptr::write` which is a force write to memory without dropping what "exists", and it's commonly used in uninitialized memory, such as uninitialized dynamic fields.
|
||||||
|
|
||||||
## Key Characteristics
|
## Key Characteristics
|
||||||
|
|
||||||
- **Prefix Pointer Syntax**: `*Type` for shared references, `*mut Type` for mutable references.
|
- **Prefix Pointer Syntax**: `*Type` for shared references, `*mut Type` for mutable references.
|
||||||
|
|||||||
Reference in New Issue
Block a user