Skip to content

[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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/DynamicQuantities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ include("internal_utils.jl")
include("fixed_rational.jl")
include("types.jl")
include("utils.jl")
include("wildcard.jl")
include("math.jl")
include("arrays.jl")
include("units.jl")
Expand Down
2 changes: 1 addition & 1 deletion src/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ for (type, base_type, _) in ABSTRACT_QUANTITY_TYPES, op in (:+, :-)
@eval begin
function Base.$op(l::$type, r::$type)
dimension(l) == dimension(r) || throw(DimensionError(l, r))
return new_quantity(typeof(l), $op(ustrip(l), ustrip(r)), dimension(l))
return new_quantity(typeof(l), $op(ustrip(l), ustrip(r)), combine_dimension_with_wildcard(l, r))
end
function Base.$op(l::$type, r::$base_type)
iszero(dimension(l)) || throw(DimensionError(l, r))
Expand Down
1 change: 0 additions & 1 deletion src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ Base.one(::D) where {D<:AbstractDimensions} = one(D)
# Additive identities (zero)
Base.zero(q::Q) where {Q<:UnionAbstractQuantity} = new_quantity(Q, zero(ustrip(q)), dimension(q))
Base.zero(::AbstractDimensions) = error("There is no such thing as an additive identity for a `AbstractDimensions` object, as + is only defined for `UnionAbstractQuantity`.")
Base.zero(::Type{<:UnionAbstractQuantity}) = error("Cannot create an additive identity for a `UnionAbstractQuantity` type, as the dimensions are unknown. Please use `zero(::UnionAbstractQuantity)` instead.")
Base.zero(::Type{<:AbstractDimensions}) = error("There is no such thing as an additive identity for a `AbstractDimensions` type, as + is only defined for `UnionAbstractQuantity`.")

# Dimensionful 1 (oneunit)
Expand Down
29 changes: 29 additions & 0 deletions src/wildcard.jl
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())
Copy link
Collaborator

@gaurav-arya gaurav-arya Nov 2, 2023

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:

using DynamicQuantities

x = 1u"ms"
arr = [zero(typeof(x))]
arr[1] = x

(Right now, it gives a StackOverflow error, but I presume that's just because the PR is WIP.)


# 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
Copy link
Collaborator

@gaurav-arya gaurav-arya Nov 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These lines seem too dangerous as discussed. Maybe getproperty should just be replaced with an error: (this is not very carefully considered, just a thought)

Suggested change
Base.getproperty(::WildcardDimensions, ::Symbol) = zero(Bool)
Base.iszero(::WildcardDimensions) = true
Base.getproperty(::WildcardDimensions, ::Symbol) = error("Cannot get dimensions of quantity with WildcardDimensions. This is probably because the quantity was created via `zero(T)` for a type `T <: AbstractQuantity`. Instead, use `zero(x)` where `x` is an `AbstractQuantity` with the desired dimensions.")


# 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, "[*]")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If dimension(x) is printed with x having WildcardDimensions, this looks like an array (totally unimportant since this is WIP, but just writing before I forget)


# + 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