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
ID:2755190
Dec 30 2021, 9:31 pm
|
|||||||
| |||||||
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 |
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 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 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 |
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.
|
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.