Created
February 21, 2016 11:11
-
-
Save PhilipWitte/dd6c670fca3baf573490 to your computer and use it in GitHub Desktop.
Example of how to get a unique ID per type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tables | |
# --- | |
type ID = int | |
var nextTypeID {.compileTime.}: ID | |
proc typeID(T:typedesc): ID = | |
const id = nextTypeID | |
static: | |
inc nextTypeID | |
return id | |
# --- | |
type Event = proc(): string | |
var registeredEvents = newTable[ID,Event]() | |
proc register(T:typedesc, event:Event) = | |
registeredEvents[typeID(T)] = event | |
# --- | |
type | |
Foo = object | |
Bar = object | |
proc aaa: string = "AAA" | |
proc bbb: string = "BBB" | |
Foo.register(aaa) | |
Bar.register(bbb) | |
for id, event in registeredEvents: | |
echo id, ": ", event() | |
# PRINTS: | |
# 1: BBB | |
# 0: AAA |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment