Also, to maintain type stability, you might consider setting a’s element type to a concrete type. UnitRange is a UnionAll, and to specify a concrete type you need to parameterize it with the type of its elements—in this case, UnitRange{Int}:
The reason it won’t broadcast is because the range has a length and so will try to broadcast over that length. The Ref works because this has a length one.
As an alternative to the Ref which may in some situations be allocating, you can also wrap it as a 1-tuple or in the StaticArrays Scalar made specifically for this reason:
I prefer Scalar as this is more semantic - the next person looking at your code doesn’t have to work out why you used a Ref or a tuple, the reason is right there in the documentation for Scalar.
Huh, didn’t know about Scalar. Definitely feels like something that should be in Base; the Ref trick to cause scalar broadcasting always felt like an ugly hack, especially since you aren’t using it for its mutability.
for tuples just doing (x) isn’t possible because parens are used to group expressions as well, while [x] will create a 1-element vector. You normally don’t want to use [x] though because that has to allocate, hence why people go for (x,), Ref, or Scalar as discussed.