ID:2024814
 
Redundant
Applies to:DM Language
Status: Redundant

This feature has already been implemented, or is already achievable with existing methods.
I find myself using this alot.

proc//a list proc
AddTo(element)//only adds to a list if that entry isn't already present.
if(!list.Find(element))
list+=element
return 1
else
return null


Pretty simple. Just gets a bit tiring to type 3-5 lines instead of one so I thought it would be a nice, small addition.
As a workaround, you can use the |= operator to add an item to a list if the item isn't already in the list.
In response to LordAndrew
oh sweet. didn't know that, thanks!
I think I made an equivalent feature request in the past for a list function that does |= and returns 1 if it adds something to the list
If you don't need the return value,
list.Find(element) || list.Add(element)
IMO, this would suit you better:

proc/AddToList(list/L, item)
if(!L) return list(item)
L |= item
return L

This way you can pass in a null list and get a new list back, so the list doesn't have to exist already. And if you don't care about associated values, then this should be even faster:

proc/AddToList(list/L, item)
if(!L) return list(item)
L[item] = null
return L

But fastest of all:

#define AddToList(L,i) {if(L)L[i]=null;else L=list(i)}

I might be missing a fine point of syntax on that last one, as I haven't tested it.
In response to Lummox JR
Yea I use something like that, the main point of the post was to suggest an AddTo proc for the /list datum itself.

Sorry if that was unclear.
Would suck a mean dick for operator overloading and list prototype modification.

I know it's like, unlikely as hell, but still...
In response to Ter13
Ter13 wrote:
Would suck a mean dick for operator overloading and list prototype modification.

I know it's like, unlikely as hell, but still...

Actually I've given operator overloading a great deal of thought. I think it'd be an interesting thing to try on datums, at least.
Actually I've given operator overloading a great deal of thought.

So... Where do we meet?
My main question on how to handle operator overloading is how I'd name the procs. The convention used for C++ is, I think, not going to cut it here.
How about looking at the names from Python for some inspiration? There's no special convention there (The __s are general in python to denote visibility).

https://docs.python.org/2/reference/ datamodel.html#special-method-names

[EDIT] Maybe something like similar names with an op_ prefix?
Typically, I use "_" as a prefix to denote "read-only" (protected), and "__" as a prefix to denote "don't ever fucking change/touch this or call this unless you know what you are doing" I.E. private.

__op_add
__op_sub
__op_or
__op_and
__op_xor

Would do, I think.
LordAndrew resolved issue (Redundant)