From 3633ff9ae9d2a91adcf2ed0ea2eb10d976ac112d Mon Sep 17 00:00:00 2001 From: Andrea Ferretti Date: Fri, 3 Jul 2015 20:59:03 +0200 Subject: [PATCH] minor --- README.md | 8 ++++---- test.nim | 15 ++++++++++++++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index e25690e..e3a554a 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ Patty - A pattern matching library ================================== -Patty is a library to perform pattern matching in Nim. The patterns have to be variant objects, which in Nim are encoded with a field (usually called `kind`) which varies in an enum, and a different object layout based on the value of this tag. An example would be +Patty is a library to perform pattern matching in Nim. The patterns have to be [variant objects](http://nim-lang.org/docs/manual.html#types-object-variants), which in Nim are encoded with a field (usually called `kind`) which varies in an enum, and a different object layout based on the value of this tag. An example would be ```nim type @@ -87,7 +87,7 @@ and expands to ```nim type ShapeE = enum - Circle, Rectangle + CircleE, RectangleE, UnitCircleE Shape = object case kind: ShapeE of CircleE: @@ -115,7 +115,7 @@ A couple of limitations fo the `adt` 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)`. -In the future, Patty will also add a proper definition of equality. +In the future, Patty will also add a proper definition of equality and copy constructors. Also, some work needs to be done to make it easier to use the generated contructors with `ref` types, in particular for the important case of recursive algebraic data types. Things that do not work (yet) ----------------------------- @@ -236,7 +236,7 @@ match c: echo "hello ", name ``` -* or combinations of patterns +* combining patterns with `or` ```nim match c: diff --git a/test.nim b/test.nim index 50abbc0..98048e6 100644 --- a/test.nim +++ b/test.nim @@ -1,4 +1,4 @@ -import unittest, patty +import unittest, patty, macros suite "adt construction": @@ -41,6 +41,19 @@ suite "adt construction": let c = UnitCircle() check c.kind == UnitCircleE + test "recusive types": + adt IntList: + Nil + Cons(head: int, tail: ref IntList) + + proc inew[A](a: A): ref A = + new(result) + result[] = a + + var d = Cons(3, inew(Cons(2, inew(Cons(1, inew(Nil())))))) + check d.head == 3 + check d.tail.head == 2 + suite "pattern matching": type ShapeKind = enum