Add a setting to specify that all string columns should be immutable
In Rails 4.2 we introduced mutation detection, to remove the need to call `attribute_will_change!` after modifying a field. One side effect of that change was that we needed to enforce that the `_before_type_cast` form of a value is a different object than the post type cast value, if the value is mutable. That contract is really only relevant for strings, but it meant we needed to dup them. In Rails 5 we added the `ImmutableString` type, to allow people to opt out of this duping in places where the memory usage was causing problems, and they don't need to mutate that field. This takes that a step further, and adds a class-level setting to specify whether strings should be frozen by default or not. The default value of this setting is `false` (strings are mutable), and I do not plan on changing that. While I think that immutable strings by default would be a reasonable default for new applications, I do not think that we would be able to document the value of this setting in a place that people will be looking when they can't figure out why some string is frozen. Realistically, the number of applications where this setting is relevant is fairly small, so I don't think it would make sense to ever enable it by default.
Showing
想要评论请 注册 或 登录