Renamed macro
This commit is contained in:
parent
aa68f7be50
commit
ff1d10be30
3 changed files with 10 additions and 10 deletions
|
|
@ -76,7 +76,7 @@ Notice that Patty requires the field you dispatch on to be called `kind`. Also,
|
|||
Patty also provides another macro to create algebraic data types. It looks like
|
||||
|
||||
```nim
|
||||
adt Shape:
|
||||
variant Shape:
|
||||
Circle(r: float)
|
||||
Rectangle(w: float, h: float)
|
||||
UnitCircle
|
||||
|
|
@ -122,7 +122,7 @@ proc UnitCircle(side: int): Shape =
|
|||
|
||||
Notice that the macro also generates three convenient constructors (`Circle` ,`Rectangle` and `UnitCircle`), and in fact the names in the enum are `CircleE`, `RectangleE` and `UnitCircleE` to avoid a name conflict. Also, a proper definition of equality based on the actual contents of the record is generated.
|
||||
|
||||
A couple of limitations fo the `adt` macro:
|
||||
A couple of limitations fo the `variant` macro:
|
||||
|
||||
* field names must be unique across branches (that is, different variants cannot have two fields with the same name). This is actually a limitation of Nim.
|
||||
* the shortcut that groups field names by type does not seem to work, that is, in the above example one could not write `Rectangle(w, h: float)`.
|
||||
|
|
|
|||
|
|
@ -151,7 +151,7 @@ proc defineEquality(tp, body: NimNode): NimNode {. compileTime .} =
|
|||
)
|
||||
# result = getAst(compare(condition, tp))
|
||||
|
||||
macro adt*(e: expr, body: stmt): stmt {. immediate .} =
|
||||
macro variant*(e: expr, body: stmt): stmt {. immediate .} =
|
||||
result = newStmtList(defineTypes(e, body), defineEquality(e, body))
|
||||
|
||||
for child in children(body):
|
||||
|
|
|
|||
14
test.nim
14
test.nim
|
|
@ -1,9 +1,9 @@
|
|||
import unittest, patty, macros
|
||||
|
||||
|
||||
suite "adt construction":
|
||||
suite "variant construction":
|
||||
test "basic creation":
|
||||
adt Shape:
|
||||
variant Shape:
|
||||
Circle(r: float, x: float, y: float)
|
||||
Rectangle(w: float, h: float)
|
||||
Square(side: int)
|
||||
|
|
@ -12,7 +12,7 @@ suite "adt construction":
|
|||
check c.r == 4.0
|
||||
|
||||
test "allowing empty objects":
|
||||
adt Shape:
|
||||
variant Shape:
|
||||
Circle(r: float, x: float, y: float)
|
||||
Rectangle(w: float, h: float)
|
||||
Square(side: int)
|
||||
|
|
@ -22,7 +22,7 @@ suite "adt construction":
|
|||
check r.h == 5.0
|
||||
|
||||
test "constructor creation":
|
||||
adt Shape:
|
||||
variant Shape:
|
||||
Circle(r: float, x: float, y: float)
|
||||
Rectangle(w: float, h: float)
|
||||
Square(side: int)
|
||||
|
|
@ -32,7 +32,7 @@ suite "adt construction":
|
|||
check c.r == 4.0
|
||||
|
||||
test "constructor of constant objects":
|
||||
adt Shape:
|
||||
variant Shape:
|
||||
Circle(r: float, x: float, y: float)
|
||||
Rectangle(w: float, h: float)
|
||||
Square(side: int)
|
||||
|
|
@ -42,7 +42,7 @@ suite "adt construction":
|
|||
check c.kind == UnitCircleE
|
||||
|
||||
test "recursive types":
|
||||
adt IntList:
|
||||
variant IntList:
|
||||
Nil
|
||||
Cons(head: int, tail: ref IntList)
|
||||
|
||||
|
|
@ -55,7 +55,7 @@ suite "adt construction":
|
|||
check d.tail.head == 2
|
||||
|
||||
test "generated equality":
|
||||
adt Shape:
|
||||
variant Shape:
|
||||
Circle(r: float, x: float, y: float)
|
||||
Rectangle(w: float, h: float)
|
||||
Square(side: int)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue