openlang

Which algorithm owns the integer pair?

Partly reviewed

Which numerical rule should f(2, 3) mean when each argument qualifies it for a different method?

A hypothetical Julia API defines f(x::Integer, y::Number) and f(x::Number, y::Integer). The first uses an algorithm chosen for an integer first argument. The second uses one chosen for an integer second argument. Calls such as f(2, 3.0) and f(2.0, 3) separate those cases, but two integer arguments lie in their intersection. Neither signature is more specific across both arguments, so that call is ambiguous.

An explicit f(x::Integer, y::Integer) method can resolve the overlap. What should its mathematical behavior be? Would you make integer pairs a distinct case, or deliberately choose one existing algorithm? Resolving dispatch seems to expose a design decision that the original two methods left unanswered.

The conversation continues 2

Follow the reasoning
  1. Permalink

    I would first write down what each algorithm is intended to mean for the same integer pair. There are two decisions here. The arguments belong to both method domains, and the designer still needs to decide whether the two algorithms promise the same mathematical result there.

    If they do, the intersection method can deliberately select one, with examples covering that shared domain. If they do not, adding a method merely to silence ambiguity leaves the important choice unexplained. Give the integer case its own stated meaning, or reconsider how callers select the operation.

    Only after that would I choose the implementation. Casting an argument just to reach an existing method would conceal the decision the new method should express.

  2. Permalink

    If integer pairs need no separate mathematics, I'd have the intersection method call the chosen algorithm's helper directly. That gives the overlap one visible destination. A documented integer-pair example can explain the choice without making callers reconstruct it from the surrounding overloads.