diff --git a/union.nim b/union.nim index 79e2cb3..fe9b558 100644 --- a/union.nim +++ b/union.nim @@ -335,8 +335,9 @@ template union*(T: untyped): untyped = ## The typeclass may contain other typeclasses, or other unions. ## ## If the typeclass contain one unique type, then that unique type will be returned. - type TImpl {.gensym.} = T - unionize(TImpl, T) + block: + type TImpl = T + unionize(TImpl, T) proc unionize(T, info: NimNode): NimNode = ## The actual union type builder diff --git a/union.nimble b/union.nimble index 7408241..873f1d4 100644 --- a/union.nimble +++ b/union.nimble @@ -1,6 +1,6 @@ # Package -version = "0.1.4" +version = "0.1.5" author = "Leorize" description = "Anonymous unions in Nim" license = "MIT" diff --git a/union/uniontraits.nim b/union/uniontraits.nim index 710985d..979197f 100644 --- a/union/uniontraits.nim +++ b/union/uniontraits.nim @@ -24,6 +24,9 @@ type ## Internally all Unions are a case object, dispatched via a compile-time ## generated enum. + UnionInst = Union[void] + ## The instance of `Union` used as parent for union types. + converter toNimNode(u: UnionTy): NimNode = NimNode(u) ## Allow us to manipulate UnionTy in here. ## @@ -63,13 +66,10 @@ func newUnionType*(enumType: NimNode): UnionTy = result = UnionTy: nnkObjectTy.newTree( newEmptyNode(), # The generics portion, we don't have it - # Inherit from `Union[void]` to allow unions to be matched by a + # Inherit from an instance of `Union` to allow unions to be matched by a # T: Union constraint nnkOfInherit.newTree( - nnkBracketExpr.newTree( - bindSym"Union", - bindSym"void" - ) + bindSym"UnionInst" ), # The list of fields nnkRecList.newTree( @@ -87,8 +87,11 @@ proc getUnionType*(n: NimNode): UnionTy = ## Returns `nil` if `n` is not an union. let typ = getTypeSkip(n) if typ.kind == nnkObjectTy: - # Verify that `n` inherits from Union. - if typ.inherits.sameType(bindSym"Union"): + # Verify that `n` inherits from UnionInst. + if typ.inherits.sameType(bindSym"UnionInst"): + UnionTy(typ) + # For Nim before PR 22642 + elif typ.inherits.sameType(bindSym"Union"): UnionTy(typ) else: UnionTy(nil)