Skip to content

Instantly share code, notes, and snippets.

@PhilipWitte
Created February 21, 2016 11:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PhilipWitte/dd6c670fca3baf573490 to your computer and use it in GitHub Desktop.
Save PhilipWitte/dd6c670fca3baf573490 to your computer and use it in GitHub Desktop.
Example of how to get a unique ID per type
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