minor
This commit is contained in:
parent
d3b7711cd1
commit
3633ff9ae9
2 changed files with 18 additions and 5 deletions
|
|
@ -1,7 +1,7 @@
|
||||||
Patty - A pattern matching library
|
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
|
```nim
|
||||||
type
|
type
|
||||||
|
|
@ -87,7 +87,7 @@ and expands to
|
||||||
```nim
|
```nim
|
||||||
type
|
type
|
||||||
ShapeE = enum
|
ShapeE = enum
|
||||||
Circle, Rectangle
|
CircleE, RectangleE, UnitCircleE
|
||||||
Shape = object
|
Shape = object
|
||||||
case kind: ShapeE
|
case kind: ShapeE
|
||||||
of CircleE:
|
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.
|
* 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)`.
|
* 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)
|
Things that do not work (yet)
|
||||||
-----------------------------
|
-----------------------------
|
||||||
|
|
@ -236,7 +236,7 @@ match c:
|
||||||
echo "hello ", name
|
echo "hello ", name
|
||||||
```
|
```
|
||||||
|
|
||||||
* or combinations of patterns
|
* combining patterns with `or`
|
||||||
|
|
||||||
```nim
|
```nim
|
||||||
match c:
|
match c:
|
||||||
|
|
|
||||||
15
test.nim
15
test.nim
|
|
@ -1,4 +1,4 @@
|
||||||
import unittest, patty
|
import unittest, patty, macros
|
||||||
|
|
||||||
|
|
||||||
suite "adt construction":
|
suite "adt construction":
|
||||||
|
|
@ -41,6 +41,19 @@ suite "adt construction":
|
||||||
let c = UnitCircle()
|
let c = UnitCircle()
|
||||||
check c.kind == UnitCircleE
|
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":
|
suite "pattern matching":
|
||||||
type
|
type
|
||||||
ShapeKind = enum
|
ShapeKind = enum
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue