-
Notifications
You must be signed in to change notification settings - Fork 25
[WIP] Create wildcard dimensions for zero(::Type) #77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,29 @@ | ||||||||
struct WildcardDimensions{R} <: AbstractDimensions{R} | ||||||||
# We simply choose Bool since it will get promoted | ||||||||
WildcardDimensions() = new{Bool}() | ||||||||
end | ||||||||
|
||||||||
constructorof(::Type{<:WildcardDimensions}) = WildcardDimensions | ||||||||
dimension_names(::Type{<:WildcardDimensions}) = () | ||||||||
|
||||||||
# Wildcard dimensions are always compatible: | ||||||||
Base.:(==)(::AbstractDimensions, ::WildcardDimensions) = true | ||||||||
Base.:(==)(::WildcardDimensions, ::AbstractDimensions) = true | ||||||||
|
||||||||
# For any zero/oneunit specified on the type, we return a wildcard dimension instead: | ||||||||
Base.zero(::Type{Q}) where {T,Q<:UnionAbstractQuantity{T}} = constructorof(Q)(zero(T), WildcardDimensions()) | ||||||||
|
||||||||
# For propagation rules, we assume all relevant dimensions are zero | ||||||||
Base.getproperty(::WildcardDimensions, ::Symbol) = zero(Bool) | ||||||||
Base.iszero(::WildcardDimensions) = true | ||||||||
Comment on lines
+17
to
+18
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These lines seem too dangerous as discussed. Maybe
Suggested change
|
||||||||
|
||||||||
# Always promote to the other dimension type: | ||||||||
Base.promote_rule(::Type{D}, ::Type{<:WildcardDimensions}) where {D<:AbstractDimensions} = D | ||||||||
|
||||||||
# Other utility rules: | ||||||||
Base.show(io::IO, ::WildcardDimensions) = print(io, "[*]") | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If |
||||||||
|
||||||||
# + and - will remove wildcard dimensions: | ||||||||
combine_dimension_with_wildcard(q1::UnionAbstractQuantity, q2::UnionAbstractQuantity) = combine_dimension_with_wildcard(dimension(q1), dimension(q2)) | ||||||||
combine_dimension_with_wildcard(d::AbstractDimensions, ::AbstractDimensions) = d | ||||||||
combine_dimension_with_wildcard(::WildcardDimensions, d::AbstractDimensions) = d |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if the right type for the returned value should be along the lines of
Quantity{T, Union{WildcardDimensions, Dimensions}}
. So that code such as the below would work:(Right now, it gives a
StackOverflow
error, but I presume that's just because the PR is WIP.)