Skip to content

Commit

Permalink
docs: add some clarifications to methods.md (#51938)
Browse files Browse the repository at this point in the history
Closes #40650
Rebase of #40667 with edits,
since upstream deleted their repo

Co-authored-by: Patrick Toche <contact@patricktoche.com>
  • Loading branch information
vtjnash and ptoche committed Nov 1, 2023
1 parent 2adf54a commit 5db9dbd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion doc/src/manual/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ There are three possible points of return from this function, returning the valu
expressions, depending on the values of `x` and `y`. The `return` on the last line could be omitted
since it is the last expression.

### Return type
### [Return type](@id man-functions-return-type)

A return type can be specified in the function declaration using the `::` operator. This converts
the return value to the specified type.
Expand Down
30 changes: 22 additions & 8 deletions doc/src/manual/methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,11 @@ Stacktrace:
[...]
```

Here the call `g(2.0, 3.0)` could be handled by either the `g(Float64, Any)` or the `g(Any, Float64)`
method, and neither is more specific than the other. In such cases, Julia raises a [`MethodError`](@ref)
rather than arbitrarily picking a method. You can avoid method ambiguities by specifying an appropriate
method for the intersection case:
Here the call `g(2.0, 3.0)` could be handled by either the `g(::Float64, ::Any)` or the
`g(::Any, ::Float64)` method. The order in which the methods are defined does not matter and
neither is more specific than the other. In such cases, Julia raises a
[`MethodError`](@ref) rather than arbitrarily picking a method. You can avoid method
ambiguities by specifying an appropriate method for the intersection case:

```jldoctest gofxy
julia> g(x::Float64, y::Float64) = 2x + 2y
Expand Down Expand Up @@ -406,7 +407,20 @@ Here's an example where the method type parameter `T` is used as the type parame
type `Vector{T}` in the method signature:

```jldoctest
julia> myappend(v::Vector{T}, x::T) where {T} = [v..., x]
julia> function myappend(v::Vector{T}, x::T) where {T}
return [v..., x]
end
myappend (generic function with 1 method)
```

The type parameter `T` in this example ensures that the added element `x` is a subtype of the
existing eltype of the vector `v`.
The `where` keyword introduces a list of those constraints after the method signature definition.
This works the same for one-line definitions, as seen above, and must appear _before_ the [return
type declaration](@ref man-functions-return-type), if present, as illustrated below:

```jldoctest
julia> (myappend(v::Vector{T}, x::T)::Vector) where {T} = [v..., x]
myappend (generic function with 1 method)
julia> myappend([1,2,3],4)
Expand Down Expand Up @@ -444,9 +458,9 @@ Stacktrace:
[...]
```

As you can see, the type of the appended element must match the element type of the vector it
is appended to, or else a [`MethodError`](@ref) is raised. In the following example, the method type parameter
`T` is used as the return value:
If the type of the appended element does not match the element type of the vector it is appended to,
a [`MethodError`](@ref) is raised.
In the following example, the method's type parameter `T` is used as the return value:

```jldoctest
julia> mytypeof(x::T) where {T} = T
Expand Down

0 comments on commit 5db9dbd

Please sign in to comment.