RFC: Language Support for Traits — Yay or Nay?

I actually don’t think traits are the right solution for this one. I think this is better addressed by instead making it easier to use 1-based indices to access containers without 1-based indices, so that 1-based indexing can be considered “generic” again.

One idea is to make a 1-based index type, as illustrated by the OrdinalIndexing.jl package: e.g., v[1st], v[2nd], v[(1:5)th]. Such indices offer a 1-based indexing assurance.

Another idea is to make a 1-based indexing view, as @DNF ponders in this post: e.g. OneBasedView(v)[1:5]. This seems inconvenient, but to illustrate how it’s not so bad: you could write a function like this:

hermitian!(A) = let A = OneBasedView(A), (m, n) = size(A)
    m == n || error("Matrix must be square")
    @inbounds for i = 1:m, j = i:n
        A[i, j] = A[i, j] + A[j, i]'
        A[j, i] = A[i, j]'
    end
end

Notice that OneBasedView is called only once at the top, and the rest of the function accesses A through this view.

These possibilities are also pondered in this thread.

1 Like