Skip to content

Commit

Permalink
Document limitations of operator overloading utils (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrhill authored Aug 22, 2024
1 parent f0d36c6 commit c7355d3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
9 changes: 7 additions & 2 deletions docs/src/dev/adding_overloads.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ Having read our guide [*"How SparseConnectivityTracer works"*](@ref how-sct-work
[`Dual`](@ref SparseConnectivityTracer.Dual)
to improve the performance of your functions or to work around some of SCT's [limitations](@ref limitations).

## Avoid hand-written overloads

!!! warning "Don't overload manually"
We strongly discourage you from manually adding methods on our tracer types.
Instead, use the same mechanisms we use ourselves.
If you want to overload a `Function` that takes `Real` arguments,
we strongly discourage you from manually adding methods to your function that use our internal tracer types.

Instead, use the same code generation mechanisms that we use.
This page of the documentation shows you how.

!!! tip "Copy one of our package extensions"
The easiest way to add overloads is to copy one of our [package extensions](https://github.com/adrhill/SparseConnectivityTracer.jl/tree/main/ext) and to modify it.
Expand Down
2 changes: 2 additions & 0 deletions src/overloads/special_cases.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ function Base.rand(
rng::AbstractRNG, ::SamplerType{D}
) where {P,T<:AbstractTracer,D<:Dual{P,T}}
p = rand(rng, P)
# This unfortunately can't just return the primal value.
# Random.jl will otherwise throw "TypeError: in typeassert, expected Dual{P,T}, got a value of type P".
t = myempty(T)
return Dual(p, t)
end

0 comments on commit c7355d3

Please sign in to comment.