ID:2951555
 
Applies to:DM Language
Status: Open

Issue hasn't been assigned a status value.
A few list-mimicking overloads for other userland data structures would be helpful since we can already overload accessor syntax.

- An overload that allows datums to report their size through length(). The most important of the three in my mind.

- An overload for "if (x in y)". A would-be-nice.

- An overload for "for (x in y)" that allows datums to be interrogated for the next item to supply to the loop, and then cleanup behavior at the end. Probably the least feasible of the three but nice for writing cleaner code.


For example, I might have

/tree
var/node/__head
var/list/node/__to_visit

/tree/New()
__head = new

/tree/operator+=(entry) // just for illustration
var/node/node = __head
while (TRUE)
if (!node.data)
node.data = entry
break
if (entry > node.data)
if (!node.right)
node.right = new /node
node = node.right
else if (entry < node.data)
if (!node.left)
node.left = new /node
node = node.left

// this might need some magic parameter that can be returned
// to signal an end of items rather than just a null
/tree/proc/operator_for()
if (!__to_visit)
__to_visit = list(__head)
var/last = length(__to_visit)
if (!last)
return
var/node/node = __to_visit[last]
--__to_visit.len
if (node.right)
__to_visit += node.right
if (node.left)
__to_visit += node.left
return node

/tree/proc/operator_for_done()
__to_visit = null

// horrendous, but for the point-
/tree/proc/operator_length()
var/count = 0
for (var/node in src)
++count
return count

/tree/proc/operator_in(entry)
for (var/node/node in src)
if (node.data == entry)
return TRUE
return FALSE
If you need something specifically to help that to look the code less bad, here's my suggestion with ~ operator:
/tree/proc/operator~()
return __to_visit

/proc/foo(tree/my_tree)
world.log << "size of tree: [length(~my_tree)]
for(var/each in ~my_tree)
do_thing))

(not sure other single overloading is allowed since doc only says - and ~)
but honestly I'd want to have length or in overloading as a datum mimicing a list is handy
I'm not really bothered about the appearance of the signature, moreso the practical need to have two (or even three, since a before step might be good to have as well) overloads to handle one use case because we need to manage the iteration information ourselves ;p

Login to reply.