Skip to content

Commit 1d20519

Browse files
committed
Enable doctests the the manual
Switching to julia-repl revealed several code blocks that could trivially be turned into doctests.
1 parent d9063cb commit 1d20519

12 files changed

+59
-50
lines changed

doc/src/devdocs/cartesian.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,19 @@ is `@nref 3 A i` (as in `A[i_1,i_2,i_3]`, where the array comes first).
5353
If you're developing code with Cartesian, you may find that debugging is easier when you examine
5454
the generated code, using `macroexpand`:
5555

56-
```julia-repl
56+
```@meta
57+
DocTestSetup = quote
58+
import Base.Cartesian: @nref
59+
end
60+
```
61+
62+
```jldoctest
5763
julia> macroexpand(:(@nref 2 A i))
58-
:(A[i_1,i_2])
64+
:(A[i_1, i_2])
65+
```
66+
67+
```@meta
68+
DocTestSetup = nothing
5969
```
6070

6171
### Supplying the number of expressions

doc/src/devdocs/reflection.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ The names of `DataType` fields may be interrogated using [`fieldnames()`](@ref).
1414
given the following type, `fieldnames(Point)` returns an arrays of [`Symbol`](@ref) elements representing
1515
the field names:
1616

17-
```julia-repl
17+
```jldoctest struct_point
1818
julia> struct Point
1919
x::Int
2020
y
@@ -29,17 +29,17 @@ julia> fieldnames(Point)
2929
The type of each field in a `Point` object is stored in the `types` field of the `Point` variable
3030
itself:
3131

32-
```julia-repl
32+
```jldoctest struct_point
3333
julia> Point.types
34-
svec(Int64,Any)
34+
svec(Int64, Any)
3535
```
3636

3737
While `x` is annotated as an `Int`, `y` was unannotated in the type definition, therefore `y`
3838
defaults to the `Any` type.
3939

4040
Types are themselves represented as a structure called `DataType`:
4141

42-
```julia-repl
42+
```jldoctest struct_point
4343
julia> typeof(Point)
4444
DataType
4545
```
@@ -52,9 +52,9 @@ of these fields is the `types` field observed in the example above.
5252
The *direct* subtypes of any `DataType` may be listed using [`subtypes()`](@ref). For example,
5353
the abstract `DataType``AbstractFloat` has four (concrete) subtypes:
5454

55-
```julia-repl
55+
```jldoctest
5656
julia> subtypes(AbstractFloat)
57-
4-element Array{DataType,1}:
57+
4-element Array{Union{DataType, UnionAll},1}:
5858
BigFloat
5959
Float16
6060
Float32
@@ -83,9 +83,9 @@ the unquoted and interpolated expression (`Expr`) form for a given macro. To use
8383
`quote` the expression block itself (otherwise, the macro will be evaluated and the result will
8484
be passed instead!). For example:
8585

86-
```julia-repl
86+
```jldoctest
8787
julia> macroexpand( :(@edit println("")) )
88-
:((Base.edit)(println,(Base.typesof)("")))
88+
:((Base.edit)(println, (Base.typesof)("")))
8989
```
9090

9191
The functions `Base.Meta.show_sexpr()` and [`dump()`](@ref) are used to display S-expr style views
@@ -95,11 +95,11 @@ Finally, the [`expand()`](@ref) function gives the `lowered` form of any express
9595
particular interest for understanding both macros and top-level statements such as function declarations
9696
and variable assignments:
9797

98-
```julia-repl
98+
```jldoctest
9999
julia> expand( :(f() = 1) )
100100
:(begin
101101
$(Expr(:method, :f))
102-
$(Expr(:method, :f, :((Core.svec)((Core.svec)((Core.Typeof)(f)),(Core.svec)())), CodeInfo(:(begin # none, line 1:
102+
$(Expr(:method, :f, :((Core.svec)((Core.svec)((Core.Typeof)(f)), (Core.svec)())), CodeInfo(:(begin # none, line 1:
103103
return 1
104104
end)), false))
105105
return f

doc/src/devdocs/subarrays.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ we can define dispatch directly on `SubArray{T,N,A,I,true}` without any intermed
152152
Since this computation doesn't depend on runtime values, it can miss some cases in which the stride
153153
happens to be uniform:
154154

155-
```julia-repl
155+
```jldoctest
156156
julia> A = reshape(1:4*2, 4, 2)
157157
4×2 Base.ReshapedArray{Int64,2,UnitRange{Int64},Tuple{}}:
158158
1 5
@@ -171,7 +171,7 @@ A view constructed as `view(A, 2:2:4, :)` happens to have uniform stride, and th
171171
indexing indeed could be performed efficiently. However, success in this case depends on the
172172
size of the array: if the first dimension instead were odd,
173173

174-
```julia-repl
174+
```jldoctest
175175
julia> A = reshape(1:5*2, 5, 2)
176176
5×2 Base.ReshapedArray{Int64,2,UnitRange{Int64},Tuple{}}:
177177
1 6

doc/src/manual/arrays.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ The [`sparse()`](@ref) function is often a handy way to construct sparse matrice
768768
its input a vector `I` of row indices, a vector `J` of column indices, and a vector `V` of nonzero
769769
values. `sparse(I,J,V)` constructs a sparse matrix such that `S[I[k], J[k]] = V[k]`.
770770

771-
```julia-repl
771+
```jldoctest sparse_function
772772
julia> I = [1, 4, 3, 5]; J = [4, 7, 18, 9]; V = [1, 2, -5, 3];
773773
774774
julia> S = sparse(I,J,V)
@@ -782,12 +782,12 @@ julia> S = sparse(I,J,V)
782782
The inverse of the [`sparse()`](@ref) function is [`findn()`](@ref), which retrieves the inputs
783783
used to create the sparse matrix.
784784

785-
```julia-repl
785+
```jldoctest sparse_function
786786
julia> findn(S)
787-
([1,4,5,3],[4,7,9,18])
787+
([1, 4, 5, 3], [4, 7, 9, 18])
788788
789789
julia> findnz(S)
790-
([1,4,5,3],[4,7,9,18],[1,2,3,-5])
790+
([1, 4, 5, 3], [4, 7, 9, 18], [1, 2, 3, -5])
791791
```
792792

793793
Another way to create sparse matrices is to convert a dense matrix into a sparse matrix using

doc/src/manual/conversion-and-promotion.md

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ generally takes two arguments: the first is a type object while the second is a
4646
to that type; the returned value is the value converted to an instance of given type. The simplest
4747
way to understand this function is to see it in action:
4848

49-
```julia-repl
49+
```jldoctest
5050
julia> x = 12
5151
12
5252
@@ -66,25 +66,24 @@ julia> typeof(ans)
6666
Float64
6767
6868
julia> a = Any[1 2 3; 4 5 6]
69-
2x3 Array{Any,2}:
69+
2×3 Array{Any,2}:
7070
1 2 3
7171
4 5 6
7272
7373
julia> convert(Array{Float64}, a)
74-
2x3 Array{Float64,2}:
74+
2×3 Array{Float64,2}:
7575
1.0 2.0 3.0
7676
4.0 5.0 6.0
7777
```
7878

7979
Conversion isn't always possible, in which case a no method error is thrown indicating that `convert`
8080
doesn't know how to perform the requested conversion:
8181

82-
```julia-repl
82+
```jldoctest
8383
julia> convert(AbstractFloat, "foo")
8484
ERROR: MethodError: Cannot `convert` an object of type String to an object of type AbstractFloat
8585
This may have arisen from a call to the constructor AbstractFloat(...),
8686
since type constructors fall back to convert methods.
87-
...
8887
```
8988

9089
Some languages consider parsing strings as numbers or formatting numbers as strings to be conversions
@@ -111,7 +110,7 @@ example, since the type is a singleton, there would never be any reason to use i
111110
the body. When invoked, the method determines whether a numeric value is true or false as a boolean,
112111
by comparing it to one and zero:
113112

114-
```julia-repl
113+
```jldoctest
115114
julia> convert(Bool, 1)
116115
true
117116
@@ -196,24 +195,24 @@ any number of arguments, and returns a tuple of the same number of values, conve
196195
type, or throws an exception if promotion is not possible. The most common use case for promotion
197196
is to convert numeric arguments to a common type:
198197

199-
```julia-repl
198+
```jldoctest
200199
julia> promote(1, 2.5)
201-
(1.0,2.5)
200+
(1.0, 2.5)
202201
203202
julia> promote(1, 2.5, 3)
204-
(1.0,2.5,3.0)
203+
(1.0, 2.5, 3.0)
205204
206205
julia> promote(2, 3//4)
207-
(2//1,3//4)
206+
(2//1, 3//4)
208207
209208
julia> promote(1, 2.5, 3, 3//4)
210-
(1.0,2.5,3.0,0.75)
209+
(1.0, 2.5, 3.0, 0.75)
211210
212211
julia> promote(1.5, im)
213-
(1.5 + 0.0im,0.0 + 1.0im)
212+
(1.5 + 0.0im, 0.0 + 1.0im)
214213
215214
julia> promote(1 + 2im, 3//4)
216-
(1//1 + 2//1*im,3//4 + 0//1*im)
215+
(1//1 + 2//1*im, 3//4 + 0//1*im)
217216
```
218217

219218
Floating-point values are promoted to the largest of the floating-point argument types. Integer
@@ -253,7 +252,7 @@ Rational(n::Integer, d::Integer) = Rational(promote(n,d)...)
253252

254253
This allows calls like the following to work:
255254

256-
```julia-repl
255+
```jldoctest
257256
julia> Rational(Int8(15),Int32(-5))
258257
-3//1
259258
@@ -297,7 +296,7 @@ which, given any number of type objects, returns the common type to which those
297296
to `promote` should be promoted. Thus, if one wants to know, in absence of actual values, what
298297
type a collection of values of certain types would promote to, one can use `promote_type`:
299298

300-
```julia-repl
299+
```jldoctest
301300
julia> promote_type(Int8, UInt16)
302301
Int64
303302
```

doc/src/manual/integers-and-floating-point-numbers.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ To make common numeric formulas and expressions clearer, Julia allows variables
584584
preceded by a numeric literal, implying multiplication. This makes writing polynomial expressions
585585
much cleaner:
586586

587-
```jldoctest
587+
```jldoctest numeric-coefficients
588588
julia> x = 3
589589
3
590590
@@ -597,7 +597,7 @@ julia> 1.5x^2 - .5x + 1
597597

598598
It also makes writing exponential functions more elegant:
599599

600-
```julia-repl
600+
```jldoctest numeric-coefficients
601601
julia> 2^2x
602602
64
603603
```
@@ -607,23 +607,23 @@ negation. So `2^3x` is parsed as `2^(3x)`, and `2x^3` is parsed as `2*(x^3)`.
607607

608608
Numeric literals also work as coefficients to parenthesized expressions:
609609

610-
```julia-repl
610+
```jldoctest numeric-coefficients
611611
julia> 2(x-1)^2 - 3(x-1) + 1
612612
3
613613
```
614614

615615
Additionally, parenthesized expressions can be used as coefficients to variables, implying multiplication
616616
of the expression by the variable:
617617

618-
```julia-repl
618+
```jldoctest numeric-coefficients
619619
julia> (x-1)x
620620
6
621621
```
622622

623623
Neither juxtaposition of two parenthesized expressions, nor placing a variable before a parenthesized
624624
expression, however, can be used to imply multiplication:
625625

626-
```julia-repl
626+
```jldoctest numeric-coefficients
627627
julia> (x-1)(x+1)
628628
ERROR: MethodError: objects of type Int64 are not callable
629629

doc/src/manual/metaprogramming.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,7 @@ we returned from the definition, now with the *value* of `x`.
920920

921921
What happens if we evaluate `foo` again with a type that we have already used?
922922

923-
```julia-repl generated
923+
```jldoctest generated
924924
julia> foo(4)
925925
16
926926
```

doc/src/manual/performance-tips.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ like `m` but not for objects like `t`.
257257
Of course, all of this is true only if we construct `m` with a concrete type. We can break this
258258
by explicitly constructing it with an abstract type:
259259

260-
```julia-repl myambig2
260+
```jldoctest myambig2
261261
julia> m = MyType{AbstractFloat}(3.2)
262262
MyType{AbstractFloat}(3.2)
263263
@@ -429,7 +429,7 @@ the array type `A`.
429429
However, there's one remaining hole: we haven't enforced that `A` has element type `T`, so it's
430430
perfectly possible to construct an object like this:
431431

432-
```julia-repl
432+
```jldoctest containers2
433433
julia> b = MyContainer{Int64, UnitRange{Float64}}(UnitRange(1.3, 5.0));
434434
435435
julia> typeof(b)

doc/src/manual/running-external-programs.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ true
4949

5050
More generally, you can use [`open()`](@ref) to read from or write to an external command.
5151

52-
```julia-repl
52+
```jldoctest
5353
julia> open(`less`, "w", STDOUT) do io
5454
for i = 1:3
5555
println(io, i)

doc/src/manual/strings.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ For when a capture doesn't match, instead of a substring, `m.captures` contains
645645
position, and `m.offsets` has a zero offset (recall that indices in Julia are 1-based, so a zero
646646
offset into a string is invalid). Here is a pair of somewhat contrived examples:
647647

648-
```jldoctest
648+
```jldoctest acdmatch
649649
julia> m = match(r"(a|b)(c)?(d)", "acd")
650650
RegexMatch("acd", 1="a", 2="c", 3="d")
651651
@@ -692,7 +692,7 @@ julia> m.offsets
692692
It is convenient to have captures returned as an array so that one can use destructuring syntax
693693
to bind them to local variables:
694694

695-
```julia-repl
695+
```jldoctest acdmatch
696696
julia> first, second, third = m.captures; first
697697
"a"
698698
```

0 commit comments

Comments
 (0)