ID:2939250
 
Applies to:DM Language
Status: Open

Issue hasn't been assigned a status value.
Y'know how this:
if(a)
a.thingy()

now has ?. as useful syntactic sugar for it?
a?.thingy()


It'd be useful if there was something similar for this
var/obj/item/a = thingy
if(istype(a))
a.do_thingy()


Similar to ?., but it checks to ensure the var is the proper type at runtime. I'm not sure what the best operator for this would be, tho.
I like the 'as' operator from C#.

I've used this before, though it's missing a dynamic return type:
proc/astype(object, type)
return istype(object, type) ? object : null

astype(a, /obj/item)?.do_thingy()

I think it would have to be a built-in instruction to tell the compiler that the type must be constant and the expression returns that type of object, for the ?. operator to know what type to go with.
even just a way to combine istype & typed var assignment would be great, i've never seen a particularly convincing syntax suggestion though
The logical syntax imo would be:
var/obj/item/a ?= thing
a?.do_thingy()


Ideally we could also do this:
if(var/obj/item/a ?= thingy)
a.do_thingy() //only in here if(a) == TRUE, so no need for conditional operator


Obviously that still doesn't solve the "I don't want to declare a variable, I just want to run this proc if it happens to be this subtype" case super cleanly.

Login to reply.