Due to limitations in Nim, object default values cannot be dynamically generated. I’ve found a workaround for this and wanted to share it with everyone. Currently, I’m using this approach to generate unique IDs for some simple value objects.
# initializer.nim
type
Initializer[T] = proc (): T {.nimcall.}
converter initializer*[T](initializer: Initializer[T]): T =
initializer()
# tdemo
# initializer.nim
type
Initializer[T] = proc (): T {.nimcall.}
converter initializer*[T](initializer: Initializer[T]): T =
initializer()
# tdemo
import std/oids
import std/monotimes
# import ./initializer
type
SomeObj = object
oid: Oid = genOid
createTime: MonoTime = getMonoTime
echo SomeObj() # (oid: 68ef0d893d02f403092dc47f, createTime: 2215664448842369)
echo default(SomeObj) # (oid: 68ef0d893d02f403092dc47f, createTime: 2215664448842369)
https://play.nim-lang.org/#pasty=KavNrekg
Perhaps this is just a bug.
Perhaps this is just a bug.
Yup. :-)