-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
proposal: frozen slices / arrays / maps and derivatives (immutable subscript-able objects) #22048
Comments
To implement this proposal you'd need to store a frozen bit somewhere. And maybe a whole word where you can store the name of the goroutine that owns the frost. Where do you propose to store this bit/word? What about
Is that allowed? What if the last two statements are swapped?
Is I'm skeptical that this proposal is even implementable. Are you intending that freeze is a guarantee, or best effort? Possibly something could be done if you only want the second of those. |
What's the motivation for this proposal? I'm under the impression this is meant to parallel JavaScript's Object.freeze API, but I can't imagine the use cases for Object.freeze map very well to Go. |
I'd prefer if this information was recorded in the type system, as it is with byte[] and string.
I'd also like it if once a value was frozen, it could not be unfrozen, as that seems to defeat the purpose of the exercise.
… On 27 Sep 2017, at 01:20, Matthew Dempsky ***@***.***> wrote:
What's the motivation for this proposal?
I'm under the impression this is meant to parallel JavaScript's Object.freeze API, but I can't imagine the use cases for Object.freeze map very well to Go.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or mute the thread.
|
I think it is possible to make this feature a fully compiler implemented thing. Just like |
Ok, good data point. No runtime changes, all in the compiler. "I think it is possible..." is unfortunately not a proposal. It is a feature request. In order to make this a proposal that can be evaluated it needs to have more implementation detail. |
For your second example, If this is verified, then the compiler generates an error like |
@Spriithy FYI, I'd still appreciate if you address my question above about motivation and potential use cases. As for the proposal, I have a few clarifying questions:
Does it compile? If so, do any of the calls to
Does it print |
Hi @mdempsky, sorry for not having answered your concern. To address your question, I don't know JavaScript, so this proposal clearly isn't under cover of adding something similar to
However, all this considered, here are some more examples that demonstrate the failure of my proposal.
type stuff []int
func (s *stuff) lock() {
freeze(*s)
}
func (s *stuff) unlock() {
unfreeze(*s) // Fails...
}
func main() {
s := make([]int, 10)
g(s, s)
}
func g(t0, t1 []int) {
freeze(t0)
println(frozen(t1)) // No way this is compile time only, is it ?
} Unless someone finds a working set of rules & implementation that would solve these cases (would be a runtime thing for sure) I think this proposal is quite born dead. However, if we consider the fact that a slice can be frozen once and never unfrozen as suggested @davecheney, many things would come out way more clear and straightforward. However, since this would become a runtime state; Go would need to allocate one extra byte in slice's header as a flag whether it is frozen or not. This would create a slight overhead in the runtime as with subscripting arrays (bounds check). |
Okay, it sounds like this proposal in its current state is withdrawn, so I'm going to close the issue. If someone comes up with a solution to the limitation identified above, we can reopen. Thanks! |
Disclaimer: I have knowledge of #20443 and the following proposes a different approach to the same problem
I would like Go to introduce three new builtin functions (as are
new
andmake
for instance). The idea behind those three functions is to provide Go's runtime with an efficient temporary immutable state for subscript-able objects (i.e. types that support indexing or key addressingt[...]
). The concept is named object freezing and can be extended further to other types.The functions
The first function is
freeze(s Subscriptable)
whereSubscriptable
is a documentation-purpose abstract type. It is used to freeze a subscript-able object.The second function is
unfreeze(s Subscriptable)
and is used to unfreeze a frozen object.The third function is
frozen(s Subscriptable) bool
and returns whether an object is frozen.Freezing rules
Here is a set of rules of object freezing I can think of.
Rules Demonstration
Here I will demonstrate what it is possible (or not) using the above set of rules (using int slices).
I know this is far from a complete / as accurate as possible proposal, but I hope it will give some insights into what I have been thinking about for a while. Feel free to point at possible loopholes I might have forgotten about !
Notes:
The text was updated successfully, but these errors were encountered: