1 | [[tags: egg]] |
---|
2 | |
---|
3 | == type-extensions |
---|
4 | |
---|
5 | [[toc:]] |
---|
6 | |
---|
7 | == Description |
---|
8 | |
---|
9 | Miscellaneous extensions for CHICKEN's type system. |
---|
10 | |
---|
11 | The source for this egg is available |
---|
12 | [[https://git.foldling.org/chicken-type-extensions.git|here]]. |
---|
13 | |
---|
14 | === Requirements |
---|
15 | |
---|
16 | * [[matchable]] |
---|
17 | |
---|
18 | == Usage |
---|
19 | |
---|
20 | {{type-extensions}} should be loaded as a compiler extension with the |
---|
21 | {{-extend}} (or {{-X}}) flag to {{csc}}: |
---|
22 | |
---|
23 | $ csc -extend type-extensions <file> |
---|
24 | |
---|
25 | === API |
---|
26 | |
---|
27 | <syntax>(define-type name)</syntax> |
---|
28 | |
---|
29 | Shorthand for {{(define-type name (struct name))}}. |
---|
30 | |
---|
31 | <syntax>(define-type (name var ...) type)</syntax> |
---|
32 | |
---|
33 | Defines a complex type alias that can be used in place of {{type}}. In |
---|
34 | each usage, all instances of {{var}} in {{type}} will be replaced by the |
---|
35 | corresponding form from {{(name var ...)}}. |
---|
36 | |
---|
37 | <enscript highlight="scheme"> |
---|
38 | (define-type (pair-of a) (pair a a)) |
---|
39 | |
---|
40 | (: pair (forall (a) (a -> (pair-of a)))) |
---|
41 | (define (pair x) (cons x x)) |
---|
42 | |
---|
43 | (compiler-typecase (pair 1) |
---|
44 | ((pair-of fixnum) |
---|
45 | (print '(pair-of fixnum))) |
---|
46 | (else |
---|
47 | (print 'else))) |
---|
48 | </enscript> |
---|
49 | |
---|
50 | As with CHICKEN's built-in {{define-type}} form, type aliases defined |
---|
51 | inside a module are not visible outside of that module. |
---|
52 | |
---|
53 | === Type Syntax |
---|
54 | |
---|
55 | <syntax>(list . type)</syntax> |
---|
56 | <syntax>(list type ...)</syntax> |
---|
57 | |
---|
58 | A dotted tail or ellipsis at the end of a {{list}} type form is |
---|
59 | shorthand for a sequence of pairs followed by {{(list-of type)}}. |
---|
60 | |
---|
61 | <enscript highlight="scheme"> |
---|
62 | ;; The following types are equivalent: |
---|
63 | (define-type a (list fixnum float . number)) |
---|
64 | (define-type b (list fixnum float number ...)) |
---|
65 | (define-type c (pair fixnum (pair float (list-of number)))) |
---|
66 | </enscript> |
---|
67 | |
---|
68 | == Author |
---|
69 | |
---|
70 | [[/users/evan-hanson|Evan Hanson]] |
---|
71 | |
---|
72 | == License |
---|
73 | |
---|
74 | 3-Clause BSD |
---|