Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

document local methods, warn about branches #41013

Merged
merged 5 commits into from
May 9, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions doc/src/manual/methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -1105,4 +1105,44 @@ reduced likelihood of ambiguities. Moreover, it extends the "public"
`myfilter` interface: a user who wants to control the padding
explicitly can call the `NoPad` variant directly.

## Defining methods in local scope

You can define methods within a [local scope](@ref scope-of-variables), for example

```jldoctest
julia> function f(x)
g(y::Int) = y + x
g(y) = y - x
g
end
f (generic function with 1 method)

julia> h = f(3)
(::var"#g#6"{Int64}) (generic function with 2 methods)

julia> h(4)
7

julia> h(4.0)
1.0
```

However, you should *not* define local methods conditionally or subject to control flow, as in
```julia
function f2(inc)
if inc
g(x) = x + 1
else
g(x) = x - 1
end
end

function f3()
function g end
return g
g() = 0
end
```
as this leads to undefined behavior.
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think describing this as generally "undefined behavior "is a bit harsh. It is typically used to mean absolutely anything can happen and your system can be in an arbitrary state. I wonder if this could be softened somewhat.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about "unexpected errors"?


[^Clarke61]: Arthur C. Clarke, *Profiles of the Future* (1961): Clarke's Third Law.