Skip to content

Commit d895e37

Browse files
committed
Make readme more explicit that package maintainers should specialize
1 parent 60869a8 commit d895e37

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

README.md

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,25 @@ For example,
1818
| `ComplexF32` | `Float32` |
1919
| `Measurement{Float32}` | `Float32` |
2020
| `Dual{BigFloat}` | `BigFloat` |
21+
| `Dual{ComplexF32}` | `Float32` |
2122
| `Rational{Int8}` | `Int8` |
2223
| `Quantity{Float32, ...}` | `Float32` |
2324
| `Quantity{Measurement{Float32}, ...}` | `Float32` |
24-
| `Dual{Complex{Float32}}` | `Float32` |
2525

26-
The standard behavior is to return the *first* type parameter,
26+
Package maintainers should write a specialized method for their type.
27+
For example, to define the base numeric type for a dual number, one could write:
28+
29+
```julia
30+
import BaseType: base_numeric_type
31+
32+
base_numeric_type(::Type{Dual{T}}) where {T} = base_numeric_type(T)
33+
```
34+
35+
It is important to call `base_numeric_type` recursively like this to deal with
36+
nested numeric types such as `Quantity{Measurement{T}}`.
37+
38+
The fallback behavior of `base_numeric_type` is to return the *first* type parameter,
2739
or, if that type has parameters of its own (such as `Dual{Complex{Float32}}`),
2840
to recursively take the first type parameter until a non-parameterized type is found.
29-
30-
Packages should write a custom method for `base_numeric_type`
31-
if this behavior is incompatible with their type.
32-
For example, if you were to create a quantity-like type
33-
`Quantity{Dimensions,T}`, for a numeric type `T`,
34-
you would need to write a custom interface to return `T`.
41+
This works for the vast majority of types, but it is still preferred
42+
if package maintainers write a specialized method.

0 commit comments

Comments
 (0)