From ff1d10be30aaaa560f53d11f8699ffb4e41b4036 Mon Sep 17 00:00:00 2001 From: Andrea Ferretti Date: Sun, 5 Jul 2015 15:34:30 +0200 Subject: [PATCH] Renamed macro --- README.md | 4 ++-- patty.nim | 2 +- test.nim | 14 +++++++------- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index de99529..3f8fa0b 100644 --- a/README.md +++ b/README.md @@ -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)`. diff --git a/patty.nim b/patty.nim index 3fcfa41..41426e6 100644 --- a/patty.nim +++ b/patty.nim @@ -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): diff --git a/test.nim b/test.nim index c983401..f77f7d4 100644 --- a/test.nim +++ b/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)