ID:2101753
 
Applies to:Dream Maker
Status: Open

Issue hasn't been assigned a status value.
(requesting mostly because of https://github.com/Baystation12/Baystation12/pull/13367 )

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
The @ token is a common one for doxygen comments. You might want to avoid using that in syntax.
If I understand what you're asking correctly, you want to define some vars and such for a datum prototype, but avoid actually creating the prototype if it hasn't been encountered already in the code--although it seems like that would preclude you from defining it later in the code.

Doing this so that it was sensitive to later definitions would be tremendously difficult. In fact I'd go so far as to say I think it would be impossible.

Handling this in a way that only counted earlier definitions--which is not really very useful--is probably doable, although it would make more sense for a special preprocessor directive to handle that. I don't know yet how I would do such a thing, but the utility of only checking for an earlier definition seems very low.
In response to Lummox JR
Lummox JR wrote:
Handling this in a way that only counted earlier definitions--which is not really very useful--is probably doable, although it would make more sense for a special preprocessor directive to handle that. I don't know yet how I would do such a thing, but the utility of only checking for an earlier definition seems very low.

Quite a bit of our code is already order-dependent; having to put this code dead last in the tree would be entirely OK to gain the speed and safety vs either list-of-types (slower) or override-as-normal (might accidentally define types).

We could likely make it work without this feature (again, once http://www.byond.com/forum/?post=2091430 is sorted), but it'd make it use a dictionary of type->value associations rather than a variable on the datums, which I suspect would be significantly slower.
On the contrary, I suspect an associative list lookup, especially on a global var, would be pretty speedy.