Skip to content

Commit 34a0543

Browse files
committed
Add documentation for abstract types
1 parent c9a5daf commit 34a0543

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

docs/src/api.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ Quantity
77
Dimensions
88
```
99

10+
There are also abstract types available. There are no required
11+
functions to build an interface, most relevant functions are
12+
defined on the abstract functions (including constructors).
13+
14+
```@docs
15+
AbstractDimensions
16+
AbstractQuantity
17+
```
18+
19+
Note also that the `Quantity` object can take a custom `AbstractDimensions`
20+
as input, so there is often no need to subtype `AbstractQuantity` separately.
21+
1022
## Utilities
1123

1224
The two main general utilities for working

src/types.jl

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,38 @@ import Tricks: static_fieldnames, static_fieldtypes
33
const DEFAULT_DIM_BASE_TYPE = FixedRational{Int32,2^4 * 3^2 * 5^2 * 7}
44
const DEFAULT_VALUE_TYPE = Float64
55

6-
abstract type AbstractQuantity{T,D} end
6+
"""
7+
AbstractDimensions{R}
8+
9+
An abstract type for dimension types. `R` is the type of the exponents of the dimensions,
10+
and by default is set to `DynamicQuantities.DEFAULT_DIM_BASE_TYPE`.
11+
AbstractDimensions are used to store the dimensions of `AbstractQuantity` objects.
12+
Together these enable many operators in Base to manipulate dimensions.
13+
This type has generic constructors for creating dimension objects, so user-defined
14+
dimension types can be created by simply subtyping `AbstractDimensions`, without
15+
the need to define many other functions.
16+
17+
The key function that one could wish to overload is
18+
`DynamicQuantities.dimension_name(::AbstractDimensions, k::Symbol)` for mapping from a field name
19+
to a base unit (e.g., `length` by default maps to `m`). You may also need to overload
20+
`DynamicQuantities.constructor_of(::Type{T})` in case of non-standard construction.
21+
"""
722
abstract type AbstractDimensions{R} end
823

24+
"""
25+
AbstractQuantity{T,D}
26+
27+
An abstract type for quantities. `T` is the type of the value of the quantity,
28+
and `D` is the type of the dimensions of the quantity. By default, `D` is set to
29+
`DynamicQuantities.DEFAULT_DIM_TYPE`. `T` is inferred from the value in a calculation,
30+
but in other cases is defaulted to `DynamicQuantities.DEFAULT_VALUE_TYPE`.
31+
It is assumed that the value is stored in the `:value` field, and the dimensions
32+
object is stored in the `:dimensions` field. These fields can be accessed with
33+
`ustrip` and `dimension`, respectively. Many operators in `Base` are defined on
34+
`AbstractQuantity` objects, including `+, -, *, /, ^, sqrt, cbrt, abs`.
35+
"""
36+
abstract type AbstractQuantity{T,D} end
37+
938
"""
1039
Dimensions{R} <: AbstractDimensions{R}
1140

0 commit comments

Comments
 (0)