ID:2498542
 
Applies to:DM Language
Status: Open

Issue hasn't been assigned a status value.
Like this (THIS IS EXAMPLE):
var/list/L = list("CAT", "DOG", "GOAT")
world << L[-1]//Must output "GOAT" in world
world << L[-2]//Must output "DOG" in world.
world << L[-4]//Must be runtimed or index start from end second time, so we have two branches, it runtiming or it will return "GOAT"


Workaround:
world << L[length(L)]//Must output "GOAT" in world
world << L[length(L) - 1]//Must output "DOG" in world.

or
L[L.len]
L[abs(-2)]
gg
Which works for exactly 1 element in the entire list.
The correct workaround for L[-n] is L.len - abs(n) + 1.
var list/l = list(
"Cat","Dog","Goat","Pig"
)



mob/Login()
var n = -2
src << l[l.len-abs(n)+1]
// l.len = 4
// abs(-2) = 2
// (4 - 2) = 2 + 1 = 3
I do think there's value in this feature, especially since a lot of the string manipulation procs take negative indices.
In response to Kozuma3
Not suitable, if list will have 4 length u will get second element, not third

L[abs(-2)]

gg
It's a nice feature, especially since I cannot come up with a reasonable one-liner sans the ternary operator because our list indices start at 1.
Like
#define LIST_INDEX(l, i) (i < 0)?(l.len + i + 1):(i)
// because the following won't work for positive index:
#define LIST_INDEX(l, i) (l.len + i) % l.len // + 1
You can do it with modulo, but it's pretty tortured.
#define LIST_INDEX(l, i) (l.len + i + 1) % (l.len + 1)

You pretend it's zero-indexed but one element longer than it actually is.
I don't really recommend it, though, because it suppresses out-of-bounds errors (most of the time) and gives nonsense results instead
Wow right over my head!

But technically, -2 is out of bounds too, so where do we draw the line? Perhaps the reason negative indices isn't more widespread is precisely because it indicates an error.

Bounds checking is always performed in situations where you may be dealing with random access and we exclude them in most situations where that macro isn't useful (iteration), so it's not a huge deal if your macro wraps around if it avoid an additional conditional for hot code paths.

Now you've got me on the fence about negative indices altogether! Lol