@@ -46,7 +46,7 @@ generally takes two arguments: the first is a type object while the second is a
4646to that type; the returned value is the value converted to an instance of given type. The simplest
4747way to understand this function is to see it in action:
4848
49- ``` julia-repl
49+ ``` jldoctest
5050julia> x = 12
515112
5252
@@ -66,25 +66,24 @@ julia> typeof(ans)
6666Float64
6767
6868julia> 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
7373julia> 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
7979Conversion isn't always possible, in which case a no method error is thrown indicating that ` convert `
8080doesn't know how to perform the requested conversion:
8181
82- ``` julia-repl
82+ ``` jldoctest
8383julia> convert(AbstractFloat, "foo")
8484ERROR: MethodError: Cannot `convert` an object of type String to an object of type AbstractFloat
8585This may have arisen from a call to the constructor AbstractFloat(...),
8686since type constructors fall back to convert methods.
87- ...
8887```
8988
9089Some 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
111110the body. When invoked, the method determines whether a numeric value is true or false as a boolean,
112111by comparing it to one and zero:
113112
114- ``` julia-repl
113+ ``` jldoctest
115114julia> convert(Bool, 1)
116115true
117116
@@ -196,24 +195,24 @@ any number of arguments, and returns a tuple of the same number of values, conve
196195type, or throws an exception if promotion is not possible. The most common use case for promotion
197196is to convert numeric arguments to a common type:
198197
199- ``` julia-repl
198+ ``` jldoctest
200199julia> promote(1, 2.5)
201- (1.0,2.5)
200+ (1.0, 2.5)
202201
203202julia> promote(1, 2.5, 3)
204- (1.0,2.5,3.0)
203+ (1.0, 2.5, 3.0)
205204
206205julia> promote(2, 3//4)
207- (2//1,3//4)
206+ (2//1, 3//4)
208207
209208julia> 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
212211julia> promote(1.5, im)
213- (1.5 + 0.0im,0.0 + 1.0im)
212+ (1.5 + 0.0im, 0.0 + 1.0im)
214213
215214julia> 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
219218Floating-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
254253This allows calls like the following to work:
255254
256- ``` julia-repl
255+ ``` jldoctest
257256julia> 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
297296to ` promote ` should be promoted. Thus, if one wants to know, in absence of actual values, what
298297type a collection of values of certain types would promote to, one can use ` promote_type ` :
299298
300- ``` julia-repl
299+ ``` jldoctest
301300julia> promote_type(Int8, UInt16)
302301Int64
303302```
0 commit comments