Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I liked how the Template Numerical Toolkit implemented one-based indexing:

    // Construct matrix. Elements are not initialized.
    // To initialize all elements to zero use
    // Matrix<double> a(2, 2, 0.0).
    Matrix<double> a(2, 2);
    
    // Assign elements to first row using
    // Fortran-style one-based indexing.
    a(1,1) = 0.5; a(1,2) = 1.0;
    
    // Assign elements to second row using
    // C-style zero-based indexing.
    a[1][0] = 1.5; a[1][1] = 2.0;
-- http://www.b-a-h.com/software/cpp/scppnt.html


This syntax is available in julia as well, but I'm not sure it's a great idea to encourage mixing the two indexing behaviors, even if they have different syntax. As I hinted in the original reply, I have seen very few cases where the choice of index offset actually makes a difference. For example, loops over indices generally use `eachindex` which doesn't care about your choice of index base.


Actually, for the sort of low-level bit twiddling code that I frequently do, 1-based indexing complicates the code greatly. I also need to pass structures that contain indices back and forth to C, so the 1-based indices cause extra overhead to constantly add or subtract the size of the element. I find that it's the largest cause of bugs in my code (which is why I really want Gallium working!)


That's interesting - thanks. I don't think anyone is eager to mix indexing behaviors either.


I just realized that when I said, "this syntax is available", people might have understood it to mean, "you can use this syntax to index arrays" (which is not true for the array type defined in base). What I meant was, "you can define an array type with this indexing behavior without changing the language (since you can override the behavior of both () and [] on a particular type)".




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: