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



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?


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).


There’s a trick that achieves this result in StaticArrays, but AFAIK it’s not something that Julia guarantees:


Yes, the plan is to probably change that in the next release (