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

Python might have used array[~0] instead, where ~ is required, to indicate end-of-list 0-based indexing.

But I guess they wanted to iterate from the end back [-1] to the start [0], making it easy to implement a rotating buffer.



> Python might have used array[~0] instead

This is what was once added to C#: arr[^idx], when this ^idx is mapped to a special object, typically optimized then out. arr[^0] means the last element.


[^n] indexing is mapped to an 'Index' struct by Roslyn which can then be applied to any array or list-shaped type (it either has to expose Index-accepting indexer, or a plain integer-based indexer and Count or Length property. There really isn't much to optimize away besides the bounds check since there are no object allocations involved.

A similar approach also works for slicing the types with range operator e.g. span[start..end].




Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

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

Search: