Skip to content

Commit

Permalink
Fix doctests
Browse files Browse the repository at this point in the history
  • Loading branch information
Seelengrab committed Jun 12, 2022
1 parent ab5f350 commit 594a9eb
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 31 deletions.
5 changes: 3 additions & 2 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ julia> new(x::Float64) = 2x;
julia> @deprecate old(x::Int) new(x);
julia> methods(old)
# 1 method for generic function "old":
[1] old(x::Int64) in Main at deprecated.jl:70
# 1 method for generic function "old" from Main:
[1] old(x::Int64)
@ deprecated.jl:93
```
will define and deprecate a method `old(x::Int)` that mirrors `new(x::Int)` but will not
define nor deprecate the method `old(x::Float64)`.
Expand Down
3 changes: 2 additions & 1 deletion doc/src/devdocs/inference.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ Each statement gets analyzed for its total cost in a function called
as follows:
```jldoctest; filter=r"tuple.jl:\d+"
julia> Base.print_statement_costs(stdout, map, (typeof(sqrt), Tuple{Int},)) # map(sqrt, (2,))
map(f, t::Tuple{Any}) in Base at tuple.jl:179
map(f, t::Tuple{Any})
@ Base tuple.jl:273
0 1 ─ %1 = Base.getfield(_3, 1, true)::Int64
1 │ %2 = Base.sitofp(Float64, %1)::Float64
2 │ %3 = Base.lt_float(%2, 0.0)::Bool
Expand Down
12 changes: 10 additions & 2 deletions doc/src/manual/constructors.md
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,13 @@ However, other similar calls still don't work:
```jldoctest parametric2
julia> Point(1.5,2)
ERROR: MethodError: no method matching Point(::Float64, ::Int64)
Closest candidates are:
Point(::T, !Matched::T) where T<:Real at none:1
Point(::T, !Matched::T) where T<:Real
@ Main none:1
Stacktrace:
[...]
```

For a more general way to make all such calls work sensibly, see [Conversion and Promotion](@ref conversion-and-promotion).
Expand Down Expand Up @@ -550,8 +555,11 @@ julia> struct SummedArray{T<:Number,S<:Number}
julia> SummedArray(Int32[1; 2; 3], Int32(6))
ERROR: MethodError: no method matching SummedArray(::Vector{Int32}, ::Int32)
Closest candidates are:
SummedArray(::Vector{T}) where T at none:4
SummedArray(::Vector{T}) where T
@ Main none:4
Stacktrace:
[...]
```
Expand Down
7 changes: 6 additions & 1 deletion doc/src/manual/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -802,8 +802,13 @@ foo (generic function with 1 method)
julia> foo([1])
ERROR: MethodError: no method matching foo(::Vector{Int64})
Closest candidates are:
foo(!Matched::Vector{Real}) at none:1
foo(!Matched::Vector{Real})
@ Main none:1
Stacktrace:
[...]
```

This is because `Vector{Real}` is not a supertype of `Vector{Int}`! You can solve this problem with something
Expand Down
7 changes: 6 additions & 1 deletion doc/src/manual/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -695,8 +695,13 @@ julia> args = [1,2,3]
julia> baz(args...)
ERROR: MethodError: no method matching baz(::Int64, ::Int64, ::Int64)
Closest candidates are:
baz(::Any, ::Any) at none:1
baz(::Any, ::Any)
@ Main none:1
Stacktrace:
[...]
```

As you can see, if the wrong number of elements are in the splatted container, then the function
Expand Down
105 changes: 83 additions & 22 deletions doc/src/manual/methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,33 @@ Applying it to any other types of arguments will result in a [`MethodError`](@re
```jldoctest fofxy
julia> f(2.0, 3)
ERROR: MethodError: no method matching f(::Float64, ::Int64)
Closest candidates are:
f(::Float64, !Matched::Float64) at none:1
f(::Float64, !Matched::Float64)
@ Main none:1
Stacktrace:
[...]
julia> f(Float32(2.0), 3.0)
ERROR: MethodError: no method matching f(::Float32, ::Float64)
Closest candidates are:
f(!Matched::Float64, ::Float64) at none:1
f(!Matched::Float64, ::Float64)
@ Main none:1
Stacktrace:
[...]
julia> f(2.0, "3.0")
ERROR: MethodError: no method matching f(::Float64, ::String)
Closest candidates are:
f(::Float64, !Matched::Float64) at none:1
f(::Float64, !Matched::Float64)
@ Main none:1
Stacktrace:
[...]
julia> f("2.0", "3.0")
ERROR: MethodError: no method matching f(::String, ::String)
Expand Down Expand Up @@ -149,14 +164,25 @@ and applying it will still result in a [`MethodError`](@ref):
```jldoctest fofxy
julia> f("foo", 3)
ERROR: MethodError: no method matching f(::String, ::Int64)
Closest candidates are:
f(!Matched::Number, ::Number) at none:1
f(!Matched::Number, ::Number)
@ Main none:1
Stacktrace:
[...]
julia> f()
ERROR: MethodError: no method matching f()
Closest candidates are:
f(!Matched::Float64, !Matched::Float64) at none:1
f(!Matched::Number, !Matched::Number) at none:1
f(!Matched::Float64, !Matched::Float64)
@ Main none:1
f(!Matched::Number, !Matched::Number)
@ Main none:1
Stacktrace:
[...]
```

You can easily see which methods exist for a function by entering the function object itself in
Expand All @@ -172,9 +198,11 @@ of those methods are, use the [`methods`](@ref) function:

```jldoctest fofxy
julia> methods(f)
# 2 methods for generic function "f":
[1] f(x::Float64, y::Float64) in Main at none:1
[2] f(x::Number, y::Number) in Main at none:1
# 2 methods for generic function "f" from Main:
[1] f(x::Float64, y::Float64)
@ none:1
[2] f(x::Number, y::Number)
@ none:1
```

which shows that `f` has two methods, one taking two `Float64` arguments and one taking arguments
Expand All @@ -190,10 +218,13 @@ julia> f(x,y) = println("Whoa there, Nelly.")
f (generic function with 3 methods)
julia> methods(f)
# 3 methods for generic function "f":
[1] f(x::Float64, y::Float64) in Main at none:1
[2] f(x::Number, y::Number) in Main at none:1
[3] f(x, y) in Main at none:1
# 3 methods for generic function "f" from Main:
[1] f(x::Float64, y::Float64)
@ none:1
[2] f(x::Number, y::Number)
@ none:1
[3] f(x, y)
@ none:1
julia> f("foo", 1)
Whoa there, Nelly.
Expand Down Expand Up @@ -256,11 +287,19 @@ julia> g(2, 3.0)
8.0
julia> g(2.0, 3.0)
ERROR: MethodError: g(::Float64, ::Float64) is ambiguous. Candidates:
g(x::Float64, y) in Main at none:1
g(x, y::Float64) in Main at none:1
ERROR: MethodError: g(::Float64, ::Float64) is ambiguous.
Candidates:
g(x::Float64, y)
@ Main none:1
g(x, y::Float64)
@ Main none:1
Possible fix, define
g(::Float64, ::Float64)
Stacktrace:
[...]
```

Here the call `g(2.0, 3.0)` could be handled by either the `g(Float64, Any)` or the `g(Any, Float64)`
Expand Down Expand Up @@ -347,8 +386,11 @@ julia> myappend([1,2,3],4)
julia> myappend([1,2,3],2.5)
ERROR: MethodError: no method matching myappend(::Vector{Int64}, ::Float64)
Closest candidates are:
myappend(::Vector{T}, !Matched::T) where T at none:1
myappend(::Vector{T}, !Matched::T) where T
@ Main none:1
Stacktrace:
[...]
Expand All @@ -361,8 +403,11 @@ julia> myappend([1.0,2.0,3.0],4.0)
julia> myappend([1.0,2.0,3.0],4)
ERROR: MethodError: no method matching myappend(::Vector{Float64}, ::Int64)
Closest candidates are:
myappend(::Vector{T}, !Matched::T) where T at none:1
myappend(::Vector{T}, !Matched::T) where T
@ Main none:1
Stacktrace:
[...]
```
Expand Down Expand Up @@ -403,9 +448,15 @@ true
julia> same_type_numeric("foo", 2.0)
ERROR: MethodError: no method matching same_type_numeric(::String, ::Float64)
Closest candidates are:
same_type_numeric(!Matched::T, ::T) where T<:Number at none:1
same_type_numeric(!Matched::Number, ::Number) at none:1
same_type_numeric(!Matched::T, ::T) where T<:Number
@ Main none:1
same_type_numeric(!Matched::Number, ::Number)
@ Main none:1
Stacktrace:
[...]
julia> same_type_numeric("foo", "bar")
ERROR: MethodError: no method matching same_type_numeric(::String, ::String)
Expand Down Expand Up @@ -791,16 +842,26 @@ bar (generic function with 1 method)
julia> bar(1,2,3)
ERROR: MethodError: no method matching bar(::Int64, ::Int64, ::Int64)
Closest candidates are:
bar(::Any, ::Any, ::Any, !Matched::Any) at none:1
bar(::Any, ::Any, ::Any, !Matched::Any)
@ Main none:1
Stacktrace:
[...]
julia> bar(1,2,3,4)
(1, 2, (3, 4))
julia> bar(1,2,3,4,5)
ERROR: MethodError: no method matching bar(::Int64, ::Int64, ::Int64, ::Int64, ::Int64)
Closest candidates are:
bar(::Any, ::Any, ::Any, ::Any) at none:1
bar(::Any, ::Any, ::Any, ::Any)
@ Main none:1
Stacktrace:
[...]
```

More usefully, it is possible to constrain varargs methods by a parameter. For example:
Expand Down
7 changes: 6 additions & 1 deletion doc/src/manual/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -729,8 +729,13 @@ to `Point` have the same type. When this isn't the case, the constructor will fa
```jldoctest pointtype
julia> Point(1,2.5)
ERROR: MethodError: no method matching Point(::Int64, ::Float64)
Closest candidates are:
Point(::T, !Matched::T) where T at none:2
Point(::T, !Matched::T) where T
@ Main none:2
Stacktrace:
[...]
```

Constructor methods to appropriately handle such mixed cases can be defined, but that will not
Expand Down
3 changes: 2 additions & 1 deletion stdlib/Test/src/Test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1611,7 +1611,8 @@ Int64
julia> @code_warntype f(2)
MethodInstance for f(::Int64)
from f(a) in Main at none:1
from f(a)
@ Main none:1
Arguments
#self#::Core.Const(f)
a::Int64
Expand Down

0 comments on commit 594a9eb

Please sign in to comment.