Compile-time variables

Kayomn spam at kayomn.net
Thu Apr 5 23:53:00 UTC 2018


Hi,

I've got a scene graph which contains multiple inheriting types. 
As such, I've been tagging them with a type enum for whenever I 
need to do things such as loading a structure from binary.

Up until now I've been using an enum that looks like this:

-------------------------------------------------------------------------------
enum NodeType : uint {
	None,
	Root,
	Sprite,
	Camera
}
-------------------------------------------------------------------------------

I'm trying to implement a dynamic type ID assignment system that 
utilizes D generics to generate an incremental, unique identifier 
for each node without needing to continuously update a master 
list. This is what I have so far:

-------------------------------------------------------------------------------
alias NodeTypeID = uint;

enum NodeTypeID getNodeID() {
	static NodeTypeID lastID = 0;

	return lastID++;
}

enum NodeTypeID getNodeID(T)() {
	static NodeTypeID typeID = getNodeID();

	return typeID;
}
-------------------------------------------------------------------------------

The expectation is that this is executed at compile time, 
generating a specific function for the given generic parameter 
each time the generic is used, incrementing the static variable 
by 1 and having the compiled generic functions essentially 
contain magic number unique to its type. So, I would be able to 
employ this like so:

-------------------------------------------------------------------------------
switch (someNodeTypeID) {
	case getNodeID!(Sprite)(): // Sprite node-specific behavior.
		break;

	case getNodeID!(Camera)(): // Camera node-specific behavior.
		break;

	default: // Default behavior.
		break;
}
-------------------------------------------------------------------------------

However, I've been struggling with an error pertaining to 
getNodeType. The return statement of lastID++ is flagging the 
error
"Static variable cannot be read at compile time."

I may just be taking too much of a C++ lens to this, but to me 
this seems like it should work? Am I missing a qualifier or 
something here?


More information about the Digitalmars-d-learn mailing list