julia> v = [1,2,3,4];
julia> convert(Vector{Int}, v) === v
true # no copy was made
julia> convert(Vector{Float64}, v) === v
false # "copy" was made (i.e. new vector was created with new type)
?
There’s also this convenient syntax for implicit convert:
v2::Vector{Int} = v # doesn't copy unless necessary
Maybe I’m misunderstanding, but when I first saw this, I assumed you were looking for reinterpret. This lets you just reinterpret a region of memory as an array of some different type. For example, you could reinterpret v as a Vector{Float64}without making a copy.
In the case that the input and output types are different, I do want the call to do a type conversion, necessitating a copy. Hence why I’m using the function convert.
I still don’t understand what you’re saying. A type conversion does not necessitate a copy, and the title of this thread suggests you don’t want a copy. So what is your actual goal?
For example,
julia> A = [1, 2, 3, 4];
julia> B = reinterpret(Float64, A);
julia> C = reinterpret(Float32, B);
julia> D = reinterpret(ComplexF64, C);
makes no copies. All four arrays point to the same underlying memory; they just tell Julia to interpret that memory in four different ways.
The statements regarding conversion necessitating or not necessitating a copy really depend on what you understand “convert” to mean. I was using it to mean what the method convert does (which does necessitate a copy), and not what the method reinterpret does. And I’m happy to disagree on that meaning; I certainly don’t want to get into an argument over semantics. Fortunately others understood what I meant, and I got the answer I was looking for.