-
Notifications
You must be signed in to change notification settings - Fork 48
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
Constructing fully immutable objects iteratively #47
Comments
IIRC that's the idea behind freeze & melt/thaw: JuliaLang/julia#31630 (comment) |
While an approach like this might work for structs w/ all I think a better path forward is something like #70, or also allowing a "KeywordArgs" struct type that would pass key-value pairs as keyword arguments to struct constructors, like |
Ok, as of #113 we've lifted the strict "order" requirement for |
I've been bothered by the fact that, for iterative construction of objects, JSON3 requires a mutable struct definition (when deserializing a type).
For fully immutable types (i.e., those which do not contain references outside to other objects, don't allow
nothing
ormissing
values, etc), at least, I'm wondering if there is some workaround.For example, if
mutable structs
andstructs
were guaranteed to have the same memory layout, could one construct a mutable struct, and then reinterpret it as an immutable one? (I'm not sure.)One workaround (for fully immutable types) that I believe would work (see VideoIO.jl/src/util.jl, which modifies fields of structs allocated by ffmpeg libraries):
T
usingfieldoffset
,fieldname
, andfieldtype
.Vector{T}(undef, 1)
Vector{UInt8}
unsafe_wrap
Pseudocode might look something like this
Obviously, the part which sets the value should be wrapped in a function, and there would probably need to be some way to make it type stable, etc. And it could be brittle. But it might work... :-)
(As a broader goal, I think it would be useful to have an extendable serialization/deserialization infrastructure similar to serde.rs, which allowed easy serialization and deserialization of immutable structs.)
The text was updated successfully, but these errors were encountered: