You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm quite often in need of an unique identifier in macros to use NimNodes and other ref types in Tables and HashSets.
Looking into the implementation of == for ref types (mEqRef magic), it seems like they have a rkNodeAddr that could serve this purpose.
The current alternative is to have a compile time Id with a scheme similar to this.
typeMyRef=refobject
value: int# Cannot use when nimvm for object fields
id: int# 1. First, show that at compile-time ref objects are still considered differentstatic:
let x =MyRef(value: 0, id: 0)
let y =MyRef(value: 0, id: 0)
let z = y
assert x != y
assert x != z
assert y == z
# 2. Now the workaround needed at compile-time to store them in HashSet/HashTablesimport random, hashes
var myRefRng {.compileTime.} =initRand(0x42)
procnewRef(value: int): MyRef=newresultresult.value = value
whennimvm:
result.id = myRefRng.rand(high(int))
else: # Necessary otherwise illegal contextdiscardprochash(x: MyRef): Hash=whennimvm:
Hash(x.id)
else:
cast[Hash](x)
The text was updated successfully, but these errors were encountered:
Currently I use = in macros when I want shallow copies and copyNimTree when I want deep copy.
In my case I want ref semantics because I want to use my type at both compile-time and runtime, having different semantics would be harder to maintain.
Furthermore I'm using this to implement my own AST and reference semantics are easier to work with for scoping:
block:
let a{.compileTime.} =newRef(10)
block:
let a{.compileTime.} =newRef(10)
I'm quite often in need of an unique identifier in macros to use NimNodes and other ref types in Tables and HashSets.
Looking into the implementation of
==
for ref types (mEqRef magic), it seems like they have arkNodeAddr
that could serve this purpose.The current alternative is to have a compile time Id with a scheme similar to this.
The text was updated successfully, but these errors were encountered: