julia icon indicating copy to clipboard operation
julia copied to clipboard

[FR] make the `wrap` function public

Open nsajko opened this issue 4 months ago • 1 comments

The wrap function, added by @MasonProtter in #52049, seems like it would be generally useful. It's like a converting constructor for arrays, but non-copying. The interface is basically wrap(output_type::Type, vector_to_be_wrapped), with optional trailing arguments for the shape also being supported:

https://github.com/JuliaLang/julia/blob/895a981efc60f27c3e7a5cf1fee00f0001b00c83/base/array.jl#L3159-L3206

Some questions that come to mind:

  • What's the generic signature? Something like wrap(::Type{<:AbstractArray}, ::AbstractVector[, ::Dims])? What's the most generic way to write the doc string? Can this support things other than arrays?

  • What should happen when a non-AbstractVector AbstractArray is passed as the second argument?

  • The short and nondescript name makes me uncomfortable. Perhaps something like wrap_in or wrap_into would be better? See also: #53552

Motivation: I think it'd be nice if FixedSizeArrays.jl could add methods to wrap to support wrapping other array types, cc @giordano @oscardssmith. The method could look like this:

function Base.wrap(::Type{FixedSizeVector}, vec::Union{Vector, Memory})
    new_fixed_size_array(vec, length(vec))
end

nsajko avatar Jun 14 '25 15:06 nsajko