Issue #8909 has been updated by bascule (Tony Arcieri).


I think there's a big opportunity here for immutable collection types... at least the sort that you know at the time you declared it is immutable.

I would hate to see a syntax like this that doesn't guarantee a deeply frozen data structure, since immutable data is quite difficult to use in Ruby at present, and really I've never found frozen data structures to be particularly useful since you can never be sure if they're immutable all the way down.

I think there's a way to ensure this is the case without requiring an object traversal system. Let's go back to what Charlie was saying:

> if I want this whole thing to be frozen, do I need to do:
> [[1, 2]f, {foo: 3}f, "blah"f]f

What if this were the *only* option, i.e. if we tried to do the other thing Charlie asked about:

> [[1, 2], {foo: 3}, "blah"]f

...it raised an exception, because you asked it to make an immutable array that contained mutable contents?

This check could even be shallow and still be useful, IMO, provided we were vigilant about how things got frozen in the first place. However...

> Is there any problem with "f" suffixes always returning the same object when the contents are the same and also immutable?

For this to work the VM would need to be in some sense aware that it's frozen objects all the way down. Isn't this state that the VM can track? What if you were simply "unallowed" to make deep frozen objects with references to other objects that aren't deep frozen?
----------------------------------------
Feature #8909: Expand "f" frozen suffix to literal arrays and hashes
https://bugs.ruby-lang.org/issues/8909#change-41985

Author: headius (Charles Nutter)
Status: Feedback
Priority: Normal
Assignee: 
Category: 
Target version: 


The "f" suffix to declare a frozen string was recently accepted into 2.1, and I think it's a spectacular addition. I would very much like to see it work for literal arrays and hashes too:

[1, 2, 3, 4, 5]f

{foo: 1, bar: 2, baz: 3}f

There are many, many cases where this could reduce allocation (frozen array with literal elements would only need to be allocated once) and improve thread-safety (explicitly create frozen arrays and hashes when creating structures that might be used across threads).

Is there any reason why we could not do this? I believe both of the above syntaxes would be invalid today, as was the case with the String "f" suffix, and hopefully that means the work to add this syntax would be similar.


-- 
http://bugs.ruby-lang.org/