Skip to content

Commit e106be0

Browse files
committed
Augment convert's documentation with a brief section noting that convert's result may alias part or all of convert's second argument when that argument is a collection or composite.
1 parent e01dcf3 commit e106be0

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
@@ -8129,6 +8129,38 @@ julia> convert(Rational{Int32}, x)
81298129
julia> convert(Rational{Int64}, x)
81308130
6004799503160661//18014398509481984
81318131
```
8132+
8133+
If `T` is a collection type and `x` a collection, the result of `convert(T, x)` may alias
8134+
`x`.
8135+
```jldoctest
8136+
julia> x = Int[1,2,3];
8137+
8138+
julia> y = convert(Vector{Int}, x);
8139+
8140+
julia> y === x
8141+
true
8142+
```
8143+
Similarly, if `T` is a composite type and `x` a related instance, the result of
8144+
`convert(T, x)` may alias part or all of `x`.
8145+
```jldoctest
8146+
julia> x = speye(5);
8147+
8148+
julia> typeof(x)
8149+
SparseMatrixCSC{Float64,Int64}
8150+
8151+
julia> y = convert(SparseMatrixCSC{Float64,Int64}, x);
8152+
8153+
julia> z = convert(SparseMatrixCSC{Float32,Int64}, y);
8154+
8155+
julia> y === x
8156+
true
8157+
8158+
julia> z === x
8159+
false
8160+
8161+
julia> z.colptr === x.colptr
8162+
true
8163+
```
81328164
"""
81338165
convert
81348166

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)