Skip to content
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

add union-splitting fallback for accepted_field_type #63

Open
jrevels opened this issue Nov 3, 2022 · 3 comments
Open

add union-splitting fallback for accepted_field_type #63

jrevels opened this issue Nov 3, 2022 · 3 comments

Comments

@jrevels
Copy link
Member

jrevels commented Nov 3, 2022

julia> Legolas.@schema "example" Example

julia> Legolas.@version ExampleV1 begin id::Union{UUID,Missing} end

julia> Legolas.accepted_field_type(ExampleV1SchemaVersion(), UUID)
Union{UUID, UInt128}

julia> Legolas.accepted_field_type(ExampleV1SchemaVersion(), Union{UUID,Missing})
Union{Missing, UUID}

I have to explicitly overload accepted_field_type for Union{UUID,Missing} if I want to capture the UUID -> Union{UUID,UInt128} fallback

Though it's not incorrect at all, needing to do this is annoying and unintuitive

It'd be nice to have an accepted_field_type fallback that automatically splits union types, applies the accepted_field_type transform to each constituent type, and then re-unions the constituents

@jrevels
Copy link
Member Author

jrevels commented Nov 5, 2022

I wonder whether it's also worth implementing a similar fallback for eltypes of Arrow.jl-supported container types (Vector, Tuple, etc.) which basically exhibit this same issue

@jrevels
Copy link
Member Author

jrevels commented Dec 22, 2022

@omus called out (or, i inferred some code he wrote 😁 ) that invoke may also be useful here in some cases, e.g.:

function accepted_field_type(sv::Legolas.SchemaVersion, ::Type{T}) where T >: Missing
     A = accepted_field_type(sv, nonmissingtype(T))
     return Union{A,Missing}
end

the fully generic one might require general union-splitting though

@iamed2
Copy link
Member

iamed2 commented Nov 29, 2023

Julia 1.9+ now has typesplit which could be used for generic union-splitting (not exported unfortunately, but better than Core.Compiler.typesubtract)

nonmissingtype is just nonmissingtype(::Type{T}) where {T} = typesplit(T, Missing) now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants