-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Hashes with constructor blocks for default values don't work out-of-the box with serializiation #15313
Comments
I'm not really sure there would be a good way for |
I was already happy finding the I really like the feature of this Hash block, especially for more complex nested structures, because you can just read without having to check every second line in the code. But since the block isn't part of the Hash type, it easy to lose it. |
I don't think this is possible to implement in a reasonable, generic way. It would require serializing a function, but most serialization formats do not even support that. So I don't think there's anything we could do. Of course it's possible to define custom serialization for some specific cases in use code. But I wouldn't see that in stdlib. Also serializating hashes with default blocks seems like a bit of an odd feature to me. What do you even need that for? |
I agree that serialization of the default block is not worth the effort (for a compiled language). What I need Hash default blocks for? Well, as I tried to say in my previous comment it's really handy to reduce code bloat: kkk2v = Hash(Int32,Hash(Int32,Hash(Int32,String))).new
kkk2v[1] ||= Hash(Int32,Hash(Int32,String)).new # awkward
kkk2v[1][2] ||= Hash(Int32,String).new # awkward
kkk2v[1][2][3] = "foo"
p kkk2v
kkk2v = Hash(Int32,Hash(Int32,Hash(Int32,String))).new do |h,k|
h[k] = Hash(Int32,Hash(Int32,String)).new do |h,k|
h[k] = Hash(Int32,String).new
end
end
kkk2v[1][2][3] = "foo"
p kkk2v Or do you mean what do I need default block serialization for? Well, I need at least some mechanism to to get the code run right after deserialization, i.e. need some mechanism to install the default block - and in case of nested Hashes as above it's not just copying / monkey patching the outer default block. |
In this case it's rather easy to fix it (see comments); it case of hashes of hashes (etc.), all having such default blocks, it gets a bit awkward.
Best solution would be serialization honoring the default instance types.
I'm running Crystal 1.14.0 [dacd97b] (2024-10-09), LLVM: 18.1.6, Default target: x86_64-unknown-linux-gnu
The text was updated successfully, but these errors were encountered: