Skip to content

Commit 5315476

Browse files
tkfKeno
authored andcommitted
Document that using invoke on external functions is not forward-compatible (#33702)
1 parent 833e6bf commit 5315476

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

base/docs/basedocs.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,6 +1408,13 @@ This method allows invoking a method other than the most specific matching metho
14081408
when the behavior of a more general definition is explicitly needed (often as part of the
14091409
implementation of a more specific method of the same function).
14101410
1411+
Be careful when using `invoke` for functions that you don't write. What definition is used
1412+
for given `argtypes` is an implementation detail unless the function is explicitly states
1413+
that calling with certain `argtypes` is a part of public API. For example, the change
1414+
between `f1` and `f2` in the example below is usually considered compatible because the
1415+
change is invisible by the caller with a normal (non-`invoke`) call. However, the change is
1416+
visible if you use `invoke`.
1417+
14111418
# Examples
14121419
```jldoctest
14131420
julia> f(x::Real) = x^2;
@@ -1416,6 +1423,25 @@ julia> f(x::Integer) = 1 + invoke(f, Tuple{Real}, x);
14161423
14171424
julia> f(2)
14181425
5
1426+
1427+
julia> f1(::Integer) = Integer
1428+
f1(::Real) = Real;
1429+
1430+
julia> f2(x::Real) = _f2(x)
1431+
_f2(::Integer) = Integer
1432+
_f2(_) = Real;
1433+
1434+
julia> f1(1)
1435+
Integer
1436+
1437+
julia> f2(1)
1438+
Integer
1439+
1440+
julia> invoke(f1, Tuple{Real}, 1)
1441+
Real
1442+
1443+
julia> invoke(f2, Tuple{Real}, 1)
1444+
Integer
14191445
```
14201446
"""
14211447
invoke

0 commit comments

Comments
 (0)