Skip to content

Commit 2c45701

Browse files
authored
Merge pull request #17519 from Sacha0/convertnote
Augment convert's documentation with a note on aliasing
2 parents 22d9440 + e106be0 commit 2c45701

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

base/docs/helpdb/Base.jl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8041,6 +8041,38 @@ julia> convert(Rational{Int32}, x)
80418041
julia> convert(Rational{Int64}, x)
80428042
6004799503160661//18014398509481984
80438043
```
8044+
8045+
If `T` is a collection type and `x` a collection, the result of `convert(T, x)` may alias
8046+
`x`.
8047+
```jldoctest
8048+
julia> x = Int[1,2,3];
8049+
8050+
julia> y = convert(Vector{Int}, x);
8051+
8052+
julia> y === x
8053+
true
8054+
```
8055+
Similarly, if `T` is a composite type and `x` a related instance, the result of
8056+
`convert(T, x)` may alias part or all of `x`.
8057+
```jldoctest
8058+
julia> x = speye(5);
8059+
8060+
julia> typeof(x)
8061+
SparseMatrixCSC{Float64,Int64}
8062+
8063+
julia> y = convert(SparseMatrixCSC{Float64,Int64}, x);
8064+
8065+
julia> z = convert(SparseMatrixCSC{Float32,Int64}, y);
8066+
8067+
julia> y === x
8068+
true
8069+
8070+
julia> z === x
8071+
false
8072+
8073+
julia> z.colptr === x.colptr
8074+
true
8075+
```
80448076
"""
80458077
convert
80468078

doc/stdlib/base.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,39 @@ All Objects
387387
julia> convert(Rational{Int64}, x)
388388
6004799503160661//18014398509481984
389389

390+
If ``T`` is a collection type and ``x`` a collection, the result of ``convert(T, x)`` may alias ``x``\ .
391+
392+
.. doctest::
393+
394+
julia> x = Int[1,2,3];
395+
396+
julia> y = convert(Vector{Int}, x);
397+
398+
julia> y === x
399+
true
400+
401+
Similarly, if ``T`` is a composite type and ``x`` a related instance, the result of ``convert(T, x)`` may alias part or all of ``x``\ .
402+
403+
.. doctest::
404+
405+
julia> x = speye(5);
406+
407+
julia> typeof(x)
408+
SparseMatrixCSC{Float64,Int64}
409+
410+
julia> y = convert(SparseMatrixCSC{Float64,Int64}, x);
411+
412+
julia> z = convert(SparseMatrixCSC{Float32,Int64}, y);
413+
414+
julia> y === x
415+
true
416+
417+
julia> z === x
418+
false
419+
420+
julia> z.colptr === x.colptr
421+
true
422+
390423
.. function:: promote(xs...)
391424

392425
.. Docstring generated from Julia source

0 commit comments

Comments
 (0)