What does Object.freeze do exactly (or how can I make objects immutable)?

Looking at the C++ implementation of the native method underlying that undocumented Object freeze method, it sets the obj_permanent flag (rather than the obj_immutable one) on the object, i.e. PyrDeepFreezer::doDeepFreeze calls BecomePermanent.

So “freeze” seems to make the garbage collector ignore that object, but not actually make it immutable. I actually haven’t found a way to explicitly do the latter…

As an aside (or why I’m asking this), the logic of deepCopy, which again has a C++ implementation (in PyrDeepCopier::constructObjectArray) is to not copy any object that is both permanent and immutable.

        if (!obj->IsMarked()) {
            if (obj->obj_flags & (obj_immutable) && obj->obj_flags & (obj_permanent)) {
// more "magic" cases that avoid copying Threads, Functions etc. (and I think thus their subclasses too)
            } else {
                recurse(obj, obj->size);

I see there is a PyrGC::BecomeImmutable too but it doesn’t seem to have any calls and thus any SC language mappings. (The obj_immutable flag is set directly numerous times though in the C++ code.) So it looks like I can’t make an object immutable in a “clean” way, but it seems that I can do it by fakely subclassing Function for example, which has the same effect on deepCopy.

Bumping this - investigating the Tuning class came to Object:freeze and very curious what it actually does… tried looking around the c++ a bit but way over my head. Does anyone know?


x = "123".copy;

x.put(1, $4);  // OK


x.put(2, $8);  // Attempted write to immutable object.

// or
x = Point(1, 2);
x.y = 3;  // ERROR: Object is immutable

So the original assertion that freeze fails to make objects immutable is mistaken. (I guess the source code should have been followed a bit further: BecomePermanent does inObject->obj_flags |= obj_immutable; which certainly does make it immutable even though the function name is not BecomePermanentAndImmutable.)

I guess my question about it is whether a “permanent” object gets GCed when it’s unreachable. I don’t know. (If these are unfamiliar concepts to you, then… probably not critical for your usage. FWIW, I’ve never needed freeze in 20 years.)