-
Notifications
You must be signed in to change notification settings - Fork 558
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow to make object immutable. #1157
base: main
Are you sure you want to change the base?
Conversation
Well, it certainly seems promising. It would be an important feather in Wren's cap if we had a decent story on immutability which didn't cost too much. I think Presumably However, there might be a case for adding an Another point which occurs to me is that the I'm not sure how important it is to be able to freeze the static fields of a class or to freeze a function/fiber object. If there isn't any easy way to do this, perhaps we should just exempt them from the immutability API. |
I can imagine there are scenarios that would require to var res = createResource()
var key = res.freeze()
// use the frozen resource
res.unfreeze(key)
// mutate the resource
key = res.freeze()
// use the frozen resource again To limit the number of symbol we could use var res = createResource()
var key = res.isFrozen = true
// use the frozen resource
res.isFrozen = key
// mutate the resource
key = res.isFrozen = true
// use the frozen resource again |
I'm not keen on Looking at the C side and Turning now to what's currently implemented for the The |
I'm currently testing another possible API that is more promising. class Object {
foreign freeze() // Eternal freeze
foreign isFrozen
foreign setFrozen(isFrozen) // Reversible freeze (`true` is the default secret)
foreign setFrozen(isFrozen, secret) // Reversible freeze with user secret
} If |
FWIW, Object.freeze() is not reversible in JS though that doesn't mean we can't make it so in Wren as there are clearly scenarios where it would be useful. |
Wren lacks the ability to declare variable constants. The foolwing patch allow to make objects immutable, giving a similar functionnality. NOTE: The implemetation while functionnal for `ObjectInstance` is opt-in for foreign and primitive methods.
Updated to the new API with secret. |
Its an interesting idea, though I have two questions:
|
1. Introduction of const keyword requires compiler to be more intelligent
or requires additional symbols to route non const to const by default.
2. It has an impact, but there is room for it so at least it as no memory
impact.
|
Hi,
Here is an attempt to provide immutable values to wren by simulating a freeze API to implement some ideas from #1156.
The following API is added:
All regular object should be covered, via the interception of
ObjInstance
write access. Thought, implementation is not perfect:foreign
andprimitives
, and should be manually checked.Fn
/Closure
are immutable with real up-values. Like class static member variables, accessing global up-values do not responds to their function being frozen.Edit: Updated to handle/test functions.
Edit: Updated to a new show new API.