How to match at least one type in vararg list for method?

question

#1

Let’s say for example, you have:

Base.min(ex::Sym, exs...) = 
    reduce((x,y)->sympy_meth(:Min, x, y), ex, exs)

This will not catch anything where the Sym comes in the exs array.

For example,

  • Base.min(Sym(x), 123) -------- works
  • Base.min(404, Sym(x)) -------- does not work!

Is there a way to to say exs is treated as Sym if at least one is present?


#2

AFAIK there is no way to do this directly. For the best approach, see the family of helper functions for broadcast. In particular, depending on what you want exactly (which you did not describe fully), you need roughly something like

_mymin(T, exs...) = ...
mymin(exs...) = _mymin(calculateT(exs...), exs...)

where T is the result of some computation on types from the types of exs, done by a helper function (look at containertype, _containertype, and promote_containertype, obviously you need something much simpler).


#3

There’s a trick that achieves this result in StaticArrays, but AFAIK it’s not something that Julia guarantees: https://github.com/JuliaArrays/StaticArrays.jl/pull/136#discussion_r109543567


#4

Yes, the plan is to probably change that in the next release (https://github.com/JuliaLang/julia/issues/21026#issuecomment-306624369)