Skip to content

Commit d765e59

Browse files
authored
Remove unnecessary use of ans keyword from manual (#36202)
* Remove unnecessary use of ans keyword from manual The ans keyword adds to the cognitive load for new users reading the manual, potentially distracting their focus from the examples. This commit replaces non essential uses of ans in the manual with clear variable assignments. * Fixes wraparound integer doctest from FAQ * Mention ans variable in Getting Started doc * Restore ans variable to Getting Started doc
1 parent a51015c commit d765e59

9 files changed

+56
-59
lines changed

doc/src/manual/calling-c-and-fortran-code.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ julia> t = ccall(:clock, Int32, ())
7171
julia> t
7272
2292761
7373
74-
julia> typeof(ans)
74+
julia> typeof(t)
7575
Int32
7676
```
7777

doc/src/manual/complex-and-rational-numbers.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,10 @@ Constructing infinite rational values is acceptable:
269269
julia> 5//0
270270
1//0
271271
272-
julia> -3//0
272+
julia> x = -3//0
273273
-1//0
274274
275-
julia> typeof(ans)
275+
julia> typeof(x)
276276
Rational{Int64}
277277
```
278278

doc/src/manual/constructors.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,10 +360,10 @@ and then delegates construction to the general constructor for the case where bo
360360
successfully creates a point of type `Point{Float64}`:
361361

362362
```jldoctest parametric2
363-
julia> Point(1,2.5)
363+
julia> p = Point(1,2.5)
364364
Point{Float64}(1.0, 2.5)
365365
366-
julia> typeof(ans)
366+
julia> typeof(p)
367367
Point{Float64}
368368
```
369369

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,16 @@ julia> x = 12
6060
julia> typeof(x)
6161
Int64
6262
63-
julia> convert(UInt8, x)
63+
julia> xu = convert(UInt8, x)
6464
0x0c
6565
66-
julia> typeof(ans)
66+
julia> typeof(xu)
6767
UInt8
6868
69-
julia> convert(AbstractFloat, x)
69+
julia> xf = convert(AbstractFloat, x)
7070
12.0
7171
72-
julia> typeof(ans)
72+
julia> typeof(xf)
7373
Float64
7474
7575
julia> a = Any[1 2 3; 4 5 6]
@@ -271,10 +271,10 @@ Rational(n::Integer, d::Integer) = Rational(promote(n,d)...)
271271
This allows calls like the following to work:
272272

273273
```jldoctest
274-
julia> Rational(Int8(15),Int32(-5))
274+
julia> x = Rational(Int8(15),Int32(-5))
275275
-3//1
276276
277-
julia> typeof(ans)
277+
julia> typeof(x)
278278
Rational{Int32}
279279
```
280280

doc/src/manual/faq.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -410,16 +410,16 @@ is bounded and wraps around at either end so that adding, subtracting and multip
410410
can overflow or underflow, leading to some results that can be unsettling at first:
411411

412412
```jldoctest
413-
julia> typemax(Int)
413+
julia> x = typemax(Int)
414414
9223372036854775807
415415
416-
julia> ans+1
416+
julia> y = x+1
417417
-9223372036854775808
418418
419-
julia> -ans
419+
julia> z = -y
420420
-9223372036854775808
421421
422-
julia> 2*ans
422+
julia> 2*z
423423
0
424424
```
425425

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

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -113,63 +113,60 @@ Unsigned integers are input and output using the `0x` prefix and hexadecimal (ba
113113
determined by the number of hex digits used:
114114

115115
```jldoctest
116-
julia> 0x1
116+
julia> x = 0x1
117117
0x01
118118
119-
julia> typeof(ans)
119+
julia> typeof(x)
120120
UInt8
121121
122-
julia> 0x123
122+
julia> x = 0x123
123123
0x0123
124124
125-
julia> typeof(ans)
125+
julia> typeof(x)
126126
UInt16
127127
128-
julia> 0x1234567
128+
julia> x = 0x1234567
129129
0x01234567
130130
131-
julia> typeof(ans)
131+
julia> typeof(x)
132132
UInt32
133133
134-
julia> 0x123456789abcdef
134+
julia> x = 0x123456789abcdef
135135
0x0123456789abcdef
136136
137-
julia> typeof(ans)
137+
julia> typeof(x)
138138
UInt64
139139
140-
julia> 0x11112222333344445555666677778888
140+
julia> x = 0x11112222333344445555666677778888
141141
0x11112222333344445555666677778888
142142
143-
julia> typeof(ans)
143+
julia> typeof(x)
144144
UInt128
145145
```
146146

147147
This behavior is based on the observation that when one uses unsigned hex literals for integer
148148
values, one typically is using them to represent a fixed numeric byte sequence, rather than just
149149
an integer value.
150150

151-
Recall that the variable [`ans`](@ref) is set to the value of the last expression evaluated in
152-
an interactive session. This does not occur when Julia code is run in other ways.
153-
154151
Binary and octal literals are also supported:
155152

156153
```jldoctest
157-
julia> 0b10
154+
julia> x = 0b10
158155
0x02
159156
160-
julia> typeof(ans)
157+
julia> typeof(x)
161158
UInt8
162159
163-
julia> 0o010
160+
julia> x = 0o010
164161
0x08
165162
166-
julia> typeof(ans)
163+
julia> typeof(x)
167164
UInt8
168165
169-
julia> 0x00000000000000001111222233334444
166+
julia> x = 0x00000000000000001111222233334444
170167
0x00000000000000001111222233334444
171168
172-
julia> typeof(ans)
169+
julia> typeof(x)
173170
UInt128
174171
```
175172

@@ -289,10 +286,10 @@ The above results are all [`Float64`](@ref) values. Literal [`Float32`](@ref) va
289286
entered by writing an `f` in place of `e`:
290287

291288
```jldoctest
292-
julia> 0.5f0
289+
julia> x = 0.5f0
293290
0.5f0
294291
295-
julia> typeof(ans)
292+
julia> typeof(x)
296293
Float32
297294
298295
julia> 2.5f-4
@@ -302,10 +299,10 @@ julia> 2.5f-4
302299
Values can be converted to [`Float32`](@ref) easily:
303300

304301
```jldoctest
305-
julia> Float32(-1.5)
302+
julia> x = Float32(-1.5)
306303
-1.5f0
307304
308-
julia> typeof(ans)
305+
julia> typeof(x)
309306
Float32
310307
```
311308

@@ -319,10 +316,10 @@ julia> 0x1p0
319316
julia> 0x1.8p3
320317
12.0
321318
322-
julia> 0x.4p-1
319+
julia> x = 0x.4p-1
323320
0.125
324321
325-
julia> typeof(ans)
322+
julia> typeof(x)
326323
Float64
327324
```
328325

doc/src/manual/metaprogramming.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ an [interned string](https://en.wikipedia.org/wiki/String_interning) used as one
105105
of expressions:
106106

107107
```jldoctest
108-
julia> :foo
108+
julia> s = :foo
109109
:foo
110110
111-
julia> typeof(ans)
111+
julia> typeof(s)
112112
Symbol
113113
```
114114

@@ -354,10 +354,10 @@ Given an expression object, one can cause Julia to evaluate (execute) it at glob
354354
[`eval`](@ref):
355355

356356
```jldoctest interp1
357-
julia> :(1 + 2)
357+
julia> ex1 = :(1 + 2)
358358
:(1 + 2)
359359
360-
julia> eval(ans)
360+
julia> eval(ex1)
361361
3
362362
363363
julia> ex = :(a + b)

doc/src/manual/strings.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,24 +51,24 @@ other subtypes of `AbstractChar`, e.g. to optimize operations for other
5151
input and shown:
5252

5353
```jldoctest
54-
julia> 'x'
54+
julia> c = 'x'
5555
'x': ASCII/Unicode U+0078 (category Ll: Letter, lowercase)
5656
57-
julia> typeof(ans)
57+
julia> typeof(c)
5858
Char
5959
```
6060

6161
You can easily convert a `Char` to its integer value, i.e. code point:
6262

6363
```jldoctest
64-
julia> Int('x')
64+
julia> c = Int('x')
6565
120
6666
67-
julia> typeof(ans)
67+
julia> typeof(c)
6868
Int64
6969
```
7070

71-
On 32-bit architectures, [`typeof(ans)`](@ref) will be [`Int32`](@ref). You can convert an
71+
On 32-bit architectures, [`typeof(c)`](@ref) will be [`Int32`](@ref). You can convert an
7272
integer value back to a `Char` just as easily:
7373

7474
```jldoctest
@@ -756,10 +756,10 @@ using non-standard string literals prefixed with various identifiers beginning w
756756
basic regular expression literal without any options turned on just uses `r"..."`:
757757

758758
```jldoctest
759-
julia> r"^\s*(?:#|$)"
759+
julia> re = r"^\s*(?:#|$)"
760760
r"^\s*(?:#|$)"
761761
762-
julia> typeof(ans)
762+
julia> typeof(re)
763763
Regex
764764
```
765765

doc/src/manual/types.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ julia> function foo()
9090
end
9191
foo (generic function with 1 method)
9292

93-
julia> foo()
93+
julia> x = foo()
9494
100
9595

96-
julia> typeof(ans)
96+
julia> typeof(x)
9797
Int8
9898
```
9999

@@ -667,10 +667,10 @@ Since the type `Point{Float64}` is a concrete type equivalent to `Point` declare
667667
in place of `T`, it can be applied as a constructor accordingly:
668668

669669
```jldoctest pointtype
670-
julia> Point{Float64}(1.0, 2.0)
670+
julia> p = Point{Float64}(1.0, 2.0)
671671
Point{Float64}(1.0, 2.0)
672672

673-
julia> typeof(ans)
673+
julia> typeof(p)
674674
Point{Float64}
675675
```
676676

@@ -695,16 +695,16 @@ that reason, you can also apply `Point` itself as a constructor, provided that t
695695
of the parameter type `T` is unambiguous:
696696

697697
```jldoctest pointtype
698-
julia> Point(1.0,2.0)
698+
julia> p1 = Point(1.0,2.0)
699699
Point{Float64}(1.0, 2.0)
700700

701-
julia> typeof(ans)
701+
julia> typeof(p1)
702702
Point{Float64}
703703

704-
julia> Point(1,2)
704+
julia> p2 = Point(1,2)
705705
Point{Int64}(1, 2)
706706

707-
julia> typeof(ans)
707+
julia> typeof(p2)
708708
Point{Int64}
709709
```
710710

0 commit comments

Comments
 (0)