mirror of
https://github.com/nim-lang/Nim.git
synced 2026-08-29 01:51:43 +00:00
macros: Introduce sameType(a, b) for node types
Previously introduced node comparison `==` was working somehow wrong on nodes
returned from getType(a), comparing just ids of the symbols.
Recently introduced `==` change 47dce26886
started comparing symbol nodes pointer-wise, thus strictly. Since getType(a)
always creates new symbol pointing to the type, comparing two such nodes using
`==` always returns false, even they point to the same type.
That is why we need a new sameType macro to be able to tell if these nodes
point to the same type.
This commit is contained in:
41
tests/macros/tsametype.nim
Normal file
41
tests/macros/tsametype.nim
Normal file
@@ -0,0 +1,41 @@
|
||||
discard """
|
||||
output: '''1
|
||||
0
|
||||
1
|
||||
0
|
||||
1
|
||||
0
|
||||
1
|
||||
0
|
||||
1
|
||||
0'''
|
||||
"""
|
||||
|
||||
import macros
|
||||
|
||||
macro same(a: typedesc, b: typedesc): expr =
|
||||
newLit(a.getType[1].sameType b.getType[1])
|
||||
|
||||
echo same(int, int)
|
||||
echo same(int, float)
|
||||
|
||||
type
|
||||
SomeInt = int
|
||||
DistinctInt = distinct int
|
||||
SomeFloat = float
|
||||
DistinctFloat = distinct float
|
||||
|
||||
echo same(int, SomeInt)
|
||||
echo same(int, DistinctInt)
|
||||
echo same(float, SomeFloat)
|
||||
echo same(float, DistinctFloat)
|
||||
|
||||
type
|
||||
Obj = object of RootObj
|
||||
SubObj = object of Obj
|
||||
Other = object of RootObj
|
||||
|
||||
echo same(Obj, Obj)
|
||||
echo same(int, Obj)
|
||||
echo same(SubObj, SubObj)
|
||||
echo same(Other, Obj)
|
||||
Reference in New Issue
Block a user