Skip to content

bug: overly permissive methods causing ambiguity #190

@nsajko

Description

@nsajko

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> 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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions