Compile-time variables

ketmar ketmar at ketmar.no-ip.org
Fri Apr 6 01:14:37 UTC 2018


Kayomn wrote:

> I'll give a better example of what it is I'm trying to do.
>
> These are node types. Their contents are not important in this 
> explanation, only that they operate as a tree structure.
>
> class Node;
>
> class RootNode : Node;
>
> class SpriteNode : Node;
>
> The result of getNodeID on a specific type is always the same. A value 
> representing it that is applied during compilation. The value does not 
> matter to the programmer, only that it is unique and gets applied.
>
> ----------------------------------------------------------------------
> uint nodeId1 = getNodeID!(Node)(); // 0.
>
> uint nodeId2 = getNodeID!(SpriteNode)(); // 1.
>
> uint comparison = getNodeID!(Node)(); // 0.
>
> // True.
> if (getNodeID!(Node)() == getNodeID!(Node)()) {
>
> }
>
> // False.
> if (getNodeID!(SpriteNode)() == getNodeID!(Node)()) {
>
> }
> ----------------------------------------------------------------------

it is already done for you, free of charge.

	class Node {}
	class RootNode : Node {}
	class SpriteNode : Node {}

	void main () {
		auto nodeId1 = typeid(Node);
		auto nodeId2 = typeid(SpriteNode);
		auto comparison = typeid(Node);

		Node n = new SpriteNode();

		assert(typeid(Node) is typeid(Node)); // obviously
		assert(typeid(SpriteNode) !is typeid(Node)); // sure
		assert(typeid(Node) is nodeId1);
		assert(typeid(n) is nodeId2);
	}


More information about the Digitalmars-d-learn mailing list