lambda icon indicating copy to clipboard operation
lambda copied to clipboard

Disallow negative integers as indices

Open 71104 opened this issue 8 years ago • 2 comments

Currently, int is allowed as index for indexed types (arrays and strings). The reason we can't restrict this to uint is that we want to be able to use expressions like - n 1 as indices. The current implementation of the - operator types to int even if both operands are uint because the result of a subtraction may be negative.

A possible solution is to implement proper 32- or 64-bit unsigned integers and wrap around when a negative value is produced, but this would raise the problem of deciding the width. A possible solution to this would in turn be to provide both 32-bit and 64-bit integer types.

71104 avatar Apr 29 '16 21:04 71104

Another possible solution is to default to 32-bit width on 32-bit platform and 64-bit width on 64-bit platforms, but this would make it difficult handling 64-bit values on 32-bit platforms.

71104 avatar Apr 29 '16 21:04 71104

Based on #77, wrapping integer values is no longer an option. Numeric types must now reflect the numeric sets used in math.

Another option would be to implement semi-circular arrays, like in Python; negative indices would refer to elements from the end (i.e. -1 refers to the last element, -2 to the second-last, etc.). This would justify integer indices.

71104 avatar May 15 '16 13:05 71104