ID:2411099
 
Resolved
List access now has a ?[] operator. If the list is null, the list access will do nothing and fail without an error.
Applies to:DM Language
Status: Resolved (514.1543)

This issue has been resolved.
// imagine having a list that your code accesses heavily...
// but there's no guarantee said list is always initialized.
mob/var/somelist[]

// currently, everywhere you access the list has to be written as:
somelist && somelist[index]

// my suggestion would shorten this to:
somelist?[index]


...Normally I'd create a proc for the sake of sanity checking, but that becomes non-viable when you have many frequently used lists. For now I've resorted to always having the lists initialized, but I figured I'd toss this suggestion out there.
+

var/i
if(somelist && (i = somelist[index]))


is a pattern I type pretty much constantly.
Also +1 from me, I pretty much do exactly what Ter does all over the place.
. already works for lists, like so:
var/list/foo = list("bar")
world.log << foo.[1]


this prints out "bar"

?. is however an error:

expected var or proc name after ?. operator
I'd have liked to have this frequently in the past
Bump.
Aye
Bumping this again because I was reminded by some code that I still want this. ?. has been shown to be a micro optimization in some places which is nice in the very hot code. A similar functionality for lists would allow me to slightly speed up some code like the following

if(somelist && somelist[key])
dostuff

to
if(somelist?[key])
dostuff
It's not on the list for 513 but I'm open to it for 514.
This is implemented in 514 and will be closed once the first beta is released.
Lummox JR resolved issue with message:
List access now has a ?[] operator. If the list is null, the list access will do nothing and fail without an error.