-
Notifications
You must be signed in to change notification settings - Fork 41
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
setindex!: convert to eltype (fixes #216) #227
Conversation
This introduces the internal utility `maybe_convert_elt` used to convert values to the element type of the StructArray before assignment. This fixes errors that are caused by assigning a value that does not have the same fields as the struct but which can be converted to such a type (e.g., `T<:Real` -> `Complex{T}`). Rather than calling `convert` directly, `maybe_convert_elt` can be specialized for particular types, like LazyRow, which should not be converted.
It's worth pointing out that this really makes a lie out of https://juliaarrays.github.io/StructArrays.jl/stable/advanced/ in the part that starts |
Ah, #216 is a duplicate of #131, and this PR overlaps with #184. #184 |
Since #184,
Just to give a little bit of perspective, this package started off as the the data structure underlying IndexedTables tables. Splitting it into a separate package made sense, as this data structure is generally useful. It also respects the Tables interface in a very straightforward way, and I think that should be preserved. Of course, the problem is that some of the design decisions come from the needs of IndexedTables (which also relies on some of the internals of StructArrays, the situation is less than ideal). In particular, collecting an iterator of rows (tuples or named tuples) where some entries could have missing values is one of the tricky scenarios. StructArrays has a While custom rules for |
Thanks for the review! I agree that Pair is a bit odd. When you suggest that |
At the moment, there are both a "named" julia> using StructArrays, StaticArrays
julia> s = StructArray(rand(SVector{2, Bool}, 3))
3-element StructArray(::Vector{Bool}, ::Vector{Bool}) with eltype SVector{2, Bool}:
[0, 1]
[1, 1]
[1, 1]
julia> StructArrays.components(s)
(Bool[0, 1, 1], Bool[1, 1, 1])
julia> s[2] = (true, false) Here, it probably still works because tuples can be converted to static vectors, but I imagine in general it's safer to not attempt to convert the Allowing to pass the |
Thanks! I've updated the tests with a variant of this example.
In the current state this only converts From my standpoint this seems to be good to go, but let me know if you want more changes. |
Ah, yes, that makes perfect sense.
Looks good to me too, feel free to merge! Minor doubt (definitely not a blocker): we have methods specializing on either argument in maybe_convert_elt(::Type{T}, vals::NamedTuple) where T = T<:NamedTuple ? convert(T, vals) : vals Not sure if this really makes a difference though, or if we actually want users to add methods to this function for their types. |
That's a very good idea, thanks for suggesting it! |
Do you think this is safe for a patch release or is this a minor version bump? I think it's safe for a patch release but would love a second opinion. |
Sorry for the late reply, have been travelling without github access for the past few days. I also think patch release is fine: all existing tests passed and the test suite is reasonably thorough. |
Thanks for reviewing this! |
This introduces the internal utility
maybe_convert_elt
used toconvert values to the element type of the StructArray before
assignment. This fixes errors that are caused by assigning a value
that does not have the same fields as the struct but which can be
converted to such a type (e.g.,
T<:Real
->Complex{T}
). Ratherthan calling
convert
directly,maybe_convert_elt
can bespecialized for particular types, like LazyRow, which should not be
converted.
Fixes #216