ID:2755190
 
Applies to:Dream Maker
Status: Open

Issue hasn't been assigned a status value.
The ability to overload [][] operator and [][]=

object[x][y]


Allowing the ability to go further byond such as operator[][][] would be pretty epic too.

grid
var
list/data
New(MAX_X,MAX_Y)
data = new/list(MAX_X,MAX_Y)
proc
operator[][](a,b)
return data[a][b]
operator[][]=(a,b,c)
data[a][b] = c
I faked this before by having the normal operator[] return another object that also has an operator[] for the actual data.

Basically you have your 2D "grid" object whose operator[](y) returns the "column" object whose operator[](x) returns the value at (x, y). You can even extend it with a 3D grid returning a 2D grid at some z.

Downside is the number of objects involved, of course, but there are ways to get around that too.
Yep, a simple datum as the return can do this, with its own overrides.

I'd be open also to an operator[]& that would return a pointer in 515m although I'm not quite sure how to build in the dereferencing automatically.
In response to Kaiochao
Kaiochao wrote:
I faked this before by having the normal operator[] return another object that also has an operator[] for the actual data.

Would be cool to see how you pulled it off, it's been giving me a headache
In response to Kozuma3
In the simplest case, if you want grid[a][b] to return data[a][b]:
grid
var/list/data

New(list/data)
src.data = data

proc/operator[](a)
return data[a]

Since grid[a] returns data[a] and data[a] is some "List", grid[a][b] is just List[b].
The 2D assignment grid[a][b] = c also becomes List[b] = c naturally.
In response to Kaiochao
Your example also needs a []= operator though.

Currently the way to do a 2D access is with a temporary datum:

grid
var/list/data
var/cols, rows

proc/operator[](row)
return new/grid_row(data, row*cols)

grid_row
var/list/data
var/offset

New(list/data, offset)
src.data = data
src.offset = offset

proc/operator[](col) return data[offset+col]
proc/operator[]=(col, val) data[offset+col] = val

This has no bounds checking so it's a fairly simple implementation.
It would be awesome if you could include more arguments as needed and have them appear as arguments for the overloading.

grid
var list/data
New()
data = new/list(arglist(args))
proc
operator[](a,b,c,d,e,f,...)
return data[a][b][c][d][e][f][...]
In response to Kozuma3
That wouldn't be possible just because of how the [] operator works. Some kind of datum solution is necessary.
Just wondering since It's still on my mind.

Would it be possible to pass

object[444,1337,420]

and have it passed to the proc as

object/proc/operator[](a=444,b=1337,c=420)

?
In response to Kozuma3
No. There's no such concept as a multiple index in the language.