JuliaPhysics / JuliaPhysics/DynamicQuantities.jl

bug: overly permissive methods causing ambiguity

Open
#190 6 comments 0 reactions 0 assignees View on GitHub
Dominant language
Julia
Stars
155
Forks
32
PR merge metrics
No merged PRs in 30d

Description

Some of the methods defined in this package, such as the two-argument methods for the arithmetic functions, such as `+`, `-`, `*`, `/` have type signatures that are invalid because of being overly permissive. The issue is that ambiguity arises as soon as another package decides to do the same thing. Example:

```julia-repl
julia> using DynamicQuantities

julia> q = 0.2u"m/s"
0.2 m s⁻¹

julia> struct Z <: Number end

julia> Base.:(*)(::Number, z::Z) = z

julia> q * Z()
ERROR: MethodError: *(::Quantity{Float64, Dimensions{FixedRational{Int32, 25200}}}, ::Z) is ambiguous.

Candidates:
*(l::AbstractQuantity, r::Number)
@ DynamicQuantities ~/tmp/jl/DynamicQuantities.jl/src/math.jl:19
*(::Number, z::Z)
@ Main REPL[4]:1

Possible fix, define
*(::AbstractQuantity, ::Z)

Stacktrace:
[1] top-level scope
@ REPL[5]:1
```

The solution is to set a more strict constraint for the "other type" in the type signature. `Number` is too permissive, as (some of) the quantity types defined here also subtype `Number`.

Some of the arithmetic methods defined in this package, those defined for `AbstractGenericQuantity`, do not constrain the "other type" at all!

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.