Defining methods without binding variables

I’ve seen many examples of Julia code where methods are defined without any variable name bindings. My usage of terminology my be inaccurate here, but for example:

eltype(::Type{<:AbstractArray{T}}) where {T} = T

As far as I can tell, this is equivalent to the following code.

eltype(S::Type{<:AbstractArray{T}}) where {T} = T

But since we don’t need to do any computation with the value S, we don’t need to bind the value to this symbol, so we can just use the first way of defining the method, so long as we have keep the reference to T, which we do care about in this context.

Is my understanding correct here?

Regardless, is this pattern documented anywhere? I’ve read through the manual sections on methods, and the only places I’ve found this kind of method definition don’t explicitly call out this pattern.

There is a stackoverflow post that calls these anonymous arguments, which is a nice nomenclature.

But the same post was complaining 4 years ago that this was not explicitly documented — it seems like it should be added to the docs (unless there is a PR that I missed?). Would be an easy short PR for someone to write.

Thanks for helping find a name for this!

Would be an easy short PR for someone to write.

I’ll take a stab at this later this week.

1 Like