wlav / cppyy

Other
387 stars 39 forks source link

Reset / restart backend state #176

Open ggoretkin-bdai opened 1 year ago

ggoretkin-bdai commented 1 year ago
import cppyy

# Is there something I can put here to ensure that the cling state is reset and not get an error when re-running code?
# SyntaxError: Failed to parse the given C++ code
# input_line_39:2:7: error: redefinition of 'Shape

cppyy.cppdef("""
class Shape {
    public:
        virtual float get_area();
};

class Rectangle: public Shape {
    public:
        float width;
        float height;
        float get_area() {
            return width*height;
        }
};
""")

Is there a way to set gClingOpts->AllowRedefinition (from https://root.cern/blog/cling-declshadow/)?

wlav commented 1 year ago

Redefinitions are enabled, but only apply to variables, not to classes or functions. There's no current way to reset Cling. You can use namespaces for different versions of a class to co-exist. On the Python side, replacing one by the other works the usual way (all assignments being references).