Skip to content

Commit 9cea16c

Browse files
committed
Document constructor functions
1 parent ae6285f commit 9cea16c

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

docs/src/types.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,10 @@ SymbolicDimensions
3030
```@docs
3131
QuantityArray
3232
```
33+
34+
## Constructors
35+
36+
```@docs
37+
constructorof
38+
with_type_parameters
39+
```

src/types.jl

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,26 @@ new_quantity(::Type{Q}, l, r) where {Q<:AbstractUnionQuantity} = constructorof(Q
173173
dim_type(::Type{Q}) where {T,D<:AbstractDimensions,Q<:AbstractUnionQuantity{T,D}} = D
174174
dim_type(::Type{<:AbstractUnionQuantity}) = DEFAULT_DIM_TYPE
175175

176+
"""
177+
constructorof(::Type{<:AbstractDimensions})
178+
constructorof(::Type{<:AbstractUnionQuantity})
179+
180+
Return the constructor of the given type. This is used to create new objects
181+
of the same type as the input. Overload a method for a new type, especially
182+
if you need custom behavior.
183+
"""
176184
constructorof(::Type{<:Dimensions}) = Dimensions
177185
constructorof(::Type{<:Quantity}) = Quantity
178186
constructorof(::Type{<:GenericQuantity}) = GenericQuantity
179187

188+
"""
189+
with_type_parameters(::Type{<:AbstractDimensions}, ::Type{R})
190+
with_type_parameters(::Type{<:AbstractUnionQuantity}, ::Type{T}, ::Type{D})
191+
192+
Return the type with the given type parameters instead of the ones in the input type.
193+
This is used to get `Dimensions{R}` from input `(Dimensions{R1}, R)`, for example.
194+
Overload a method for a new type, especially if you need custom behavior.
195+
"""
180196
function with_type_parameters(::Type{<:Dimensions}, ::Type{R}) where {R}
181197
return Dimensions{R}
182198
end
@@ -188,7 +204,7 @@ function with_type_parameters(::Type{<:GenericQuantity}, ::Type{T}, ::Type{D}) w
188204
end
189205

190206
# The following functions should be overloaded for special types
191-
function constructorof(::Type{T}) where {T}
207+
function constructorof(::Type{T}) where {T<:Union{AbstractUnionQuantity,AbstractDimensions}}
192208
return Base.typename(T).wrapper
193209
end
194210
function with_type_parameters(::Type{D}, ::Type{R}) where {D<:AbstractDimensions,R}

0 commit comments

Comments
 (0)