I often find myself hesitating between these two options:
struct A{B} end
eltype(A{B}) where {B} = B
method(a::A) = do_stuff_with(a, eltype(a)) # ?
method(a::A{B}) where {B} = do_stuff_with(a, B) # ?
What's the difference? What do they imply? Are there contexts in which I should prefer either over the other?
They are the same in almost all cases, except:
B
: https://docs.julialang.org/en/v1/manual/performance-tips/#Be-aware-of-when-Julia-avoids-specializingB
, which may be necessary to avoid ambiguities in situations where you have struct A{B <:Integer} end
, where A
is a different type from A{B} where B
The style guide recommends the former, so I say: Use that, unless you are in the edge cases where you need specialization, or in that weird subtyping edge case
Last updated: Aug 14 2025 at 04:51 UTC