Object type: examples with methods saveobj(), loadobj(), delete()

Hello,

about object variables saving and loading, is there a few example how to implement saveobj(), loadobj() and delete() overloading function in classdefobject ? Currently there’s neither example nor details in the documentation.

Especially, the sentence about saveobj() is a bit obscure:

  • You have to return a native scilab variable (struct, list, …) except objects that contains all data you want to save.

thank you

David

Hello David,

The idea is to save only the properties (since the methods are in the classdef definition).

Here is a concise example:

classdef demosave
    properties
        a
        b
    end

    methods
        function demosave(a,b)
            this.a = a;
            this.b = b;
        end

        function st = saveobj()
            disp("save properties");
            st.a = this.a;
            st.b = this.b;
        end

        function loadobj(st)
            disp("reload properties")
            this.a = st.a;
            this.b = st.b;
        end

        function delete()
            disp("demosave destructor");
        end
    end
end

a = demosave(12, "test");
save("TMPDIR/save.sod", "a");
clear a; //call delete
load("TMPDIR/save.sod");
a

BUT, while writing this message, I noticed there’s a bug in loadobj: the data we receive (st in my example) doesn’t contain the struct but this instead.
I reported the issue (#17513)

Best regards,
Antoine

1 Like

Thank you Antoine, clear now !

Out of curiosity, is really needed the destructor since Scilab is not complaining when I perform a clear while there was an object variable in the memory stack ?

Maybe good to include in demo a case or a note about when it makes sense to perform special action when removing the object instance (link with a third party code for instance ?).

Thank you

David

Absolutely — use cases for a destructor will be very rare.
It might be used to release a hardware resource or close a connection, that kind of thing.
But as long as you only use standard Scilab variables, you’ll probably never need one.
Matrices, structs, lists, … will be deleted automatically when the instance is destroyed.