#How to free mutable global optional variable?

1 messages · Page 1 of 1 (latest)

nimble sleet
#
pub var currentCloneResult : ?CloneResult = null;
pub const CloneResult = union(enum) {
    Ok: std.ArrayList(CloneEntry),
    Error: std.ArrayList(CloneEntry),
};
pub const CloneEntry = union(enum) {
    GameData: GDCE,
    Language: LCE,
};
pub const GDCE = struct {
    table: common.GameDataTableType,
    oldId: u32,
    newId: u32 = 0,
};
pub const LCE = struct {
    table: common.langM.LanguageTableType,
    oldId: u32,
    newId: u32 = 0,
};

I trued this:


pub fn clone(cff: *common.CffData, gpa: std.mem.Allocator) void
{
    if (currentCloneResult) |cr| {
        switch (cr) {
            .Ok => |ok| {
                cloneImpl(cff, gpa, ok.items);
// I can not figure out a way, to get mutable acces here, to call deinit()
                // TODO
                // ok.deinit(gpa);
                currentCloneResult = null;
            },
            else => unreachable
        }
    }
}
tribal hearth
#

.Ok => |*ok|

nimble sleet
#

error: expected type '*T', found '*const T'
ok.deinit(gpa);
already tried, that is still not mutable (and LSP does not even see the type, when I add *)

twin forge
nimble sleet
#

error: expected type '*cff.clone.CloneResult', found '@typeInfo(cff.clone.CloneResult).@"union".tag_type.?'
.Ok => |*stack| {
~^~

pub fn clear(gpa: std.mem.Allocator) void {
    if (currentCloneResult) |*cr| {
        switch (cr) {
            .Ok => |*stack| {

I think the combination of ?union might be the issue then 😢

subtle juniper
twin forge
nimble sleet
# subtle juniper i think that ```switch (cr.*) {``` should help

Thank you very much this works, but LSP completely gives up on it for some reason 🤔 but compiler accepts it....

pub fn clear(gpa: std.mem.Allocator) void {
    if (currentCloneResult) |*cr| {
        switch (cr.*) {
            .Error, .Ok => |*stack| {
                stack.deinit(gpa);
                currentCloneResult = null;
            },
        }
    }
}
subtle juniper