Skip to content

Files

Latest commit

 

History

History
105 lines (78 loc) · 1.71 KB

Structures.md

File metadata and controls

105 lines (78 loc) · 1.71 KB

Structures

Structs are deallocated from memory automatically, but there is a caveat when it comes to recursive types: the same type within the same type; this must be handled by the programmer, using instructions like extract and free, manually. Since structs will always be allocated to the heap.

// Struct definition

public struct MyStruct {
    size i64
    length i32
    matter bool
};
// Struct native construction

public struct MyStruct {
    size i64
    length i32
    matter bool
};

fn main() {

    var some_struct: MyStruct = new MyStruct {
        matter true
        length 12
        size 12312
    };

}
// Struct methods

public struct MyStruct {
    size i64 // Always public field
    length i32 
    matter bool
};

for MyStruct {

    // Struct constructor
    fn new(): self {
        pass
    }

    // Public method
    public fn dealloc(): void {
        @internal dealloc(this);
    }

    // Private method
    fn set_size(new_size :: i64): void {
        this.size = new_size;
    }
}
// Struct method access 

public struct MyStruct {
    size i64 // Always public field
    length i32 
};

for MyStruct {

    // Struct constructor
    fn new(): self {
        pass
    }

    // Public method
    public fn dealloc(): void {
        @internal dealloc(this);
    }

    // Public method
    public fn set_length_and_size(length :: i64, size :: i64): void {
        this.set_size(size);
        this.length = length;
    }

    // Private method
    fn set_size(new_size :: i64): void {
        this.size = new_size;
    }
}

fn main() {

    var some_struct: MyStruct = MyStruct.new();

    some_struct.set_length_and_size(0, 0);
    some_struct.dealloc();
}