The above PR needs to add a value to various types (most of them) in the codebase; there are, I think, two ways to do it (three if http://www.byond.com/forum/?post=2091430 gets fixed);
Current methods:
Loop over list of types, istype()
// This will have strange results if eg /datum is defined in the list before /datum/foo
var/list/values = list(
/datum/foo = 2
/datum/bar = 3
)
/proc/get_value(datum/D)
for(var/type in values)
if(istype(D, type))
return values[type]
// If http://www.byond.com/forum/?post=2091430 is fixed:
// This could be done with initial(parent_type) indexing the list instead
// This would *not* have strange results, but is almost certainly slower than a var on the instances
/proc/get_value(datum/D)
var/datum/t = D.type
while(t && !(t in values))
t = initial(t.parent_type)
if(t) // t==null means we hit /datum and still didn't find it (/datum's parent_type is null)
return values[t]
Var override on types
// If a type was *not* previously defined (eg it was misspelled, or removed from the main code), this will define it with no obvious way to tell that this has happened
/datum/var/value
/datum/foo/value = 2
/datum/bar/value = 3
/proc/get_value(datum/D)
return D.value
This feature request
// With this feature, var override on types
// I am using the syntax @type/path/here to mean "define this part iff the type is already defined, otherwise error"
// I'm not hugely attached to this syntax, but I need *something* for the examples
/datum/var/value
/datum/foo
@datum/foo/value = 2 // this would be OK
@datum/bar/value = 3 // error: type /datum/bar not defined
/proc/get_value(datum/D)
return D.value
Ideally, this would apply to blocks, ie this code:
datum
one
@two
three
- would define the type /datum/one unconditionally (pointlessly, since it must exist already for the next line to pass)
- if /datum/one/two does not already exist, it would error
- if it does exist, it would define /datum/one/two/three