ID:2748216
 
Applies to:DM Language
Status: Open

Issue hasn't been assigned a status value.
Once in a while there arises a situation in which we want to get the parent type of a type without actually having an instance of that type. When it comes to regular vars on a type there is the "initial() trick" to get the initial value of that var on the type without instantiation. Sadly this trick doesn't work for constant values such as parent_type. Example below:
proc/get_parent_type(typepath)
    var/datum/dummy = typepath
    return initial(dummy.parent_type)

This currently throws a runtime error of
cannot change constant value
My proposed change is to make it so it instead returns the actual value of parent_type on that type. Applying it to other constants might be useful too.

If this is not feasible due to limitations unknown to me I instead propose a global proc to get the parent type of a type that would serve the same purpose.
As a workaround, off the top of my head, you can loop over every type that exists (using typesof()) to build a table of parent types:
proc/parent_type_of(type)
var/global/list/Table
Table ||= _parent_type_table()
return Table[type]

proc/_parent_type_table()
var/list/table = new
var/list/stack = list(/datum)
while(stack.len)
var/parent = stack[stack.len]
stack.len--
var/children = typesof(parent) - parent
for(var/child in children)
table[child] = parent
stack += children
return table

In this implementation, the table is fully generated upon first use.
Hm yeah that's true, I feel silly for not thinking of that. Potentially with deep type trees it will run in quadratic time on initialization but I reckon most type trees are wide rather than deep. Thanks.