This commit is contained in:
Andrea Ferretti 2015-07-03 20:59:03 +02:00
commit 3633ff9ae9
2 changed files with 18 additions and 5 deletions

View file

@ -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:

View file

@ -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