Skip to content

Commit

Permalink
Merge pull request #52 from alaviss/fix/issue-51
Browse files Browse the repository at this point in the history
Fixes for 0.1.5
  • Loading branch information
alaviss authored Sep 5, 2023
2 parents a5b6d31 + a9a0a12 commit 79e3f8a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
5 changes: 3 additions & 2 deletions union.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion union.nimble
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Package

version = "0.1.4"
version = "0.1.5"
author = "Leorize"
description = "Anonymous unions in Nim"
license = "MIT"
Expand Down
17 changes: 10 additions & 7 deletions union/uniontraits.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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.
##
Expand Down Expand Up @@ -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(
Expand All @@ -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)
Expand Down

0 comments on commit 79e3f8a

Please sign in to comment.