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 a similar method for Type{<:CodeUnits} #57826

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ararslan
Copy link
Member

Currently, similar(::CodeUnits) works as expected by going through the generic AbstractArray method. However, the fallback method hit by similar(::Type{<:CodeUnits}, dims) does not work, as it assumes the existence of a constructor that accepts an UndefInitializer. This can be made to work by defining a corresponding similar method that returns an Array.

One could make a case that this is a bugfix since it was arguably a bug that this method didn't work given that CodeUnits is an AbstractArray subtype and the other similar methods work. If anybody buys that argument, it could be nice to backport this; it came up in some internal code that uses Arrow.jl and JSON3.jl together.

Currently, `similar(::CodeUnits)` works as expected by going through the
generic `AbstractArray` method. However, the fallback method hit by
`similar(::Type{<:CodeUnits}, dims)` does not work, as it assumes the
existence of a constructor that accepts an `UndefInitializer`. This can
be made to work by defining a corresponding method that returns an
`Array`.
@ararslan ararslan added the strings "Strings!" label Mar 18, 2025
@ararslan ararslan requested a review from KristofferC March 20, 2025 17:16
@@ -805,6 +805,8 @@ write(io::IO, s::CodeUnits) = write(io, s.s)
cconvert(::Type{Ptr{T}}, s::CodeUnits{T}) where {T} = cconvert(Ptr{T}, s.s)
cconvert(::Type{Ptr{Int8}}, s::CodeUnits{UInt8}) = cconvert(Ptr{Int8}, s.s)

similar(::Type{<:CodeUnits{T}}, dims::Dims) where {T} = Array{T}(undef, dims)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative (possibly better?) solution could also be

Suggested change
similar(::Type{<:CodeUnits{T}}, dims::Dims) where {T} = Array{T}(undef, dims)
similar(::Type{<:CodeUnits{T}}, dims::Dims) where {T} = similar(Array{T}, dims)

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

Successfully merging this pull request may close these issues.

1 participant