source: project/wiki/eggref/5/type-extensions @ 36452

Last change on this file since 36452 was 36452, checked in by evhan, 10 months ago

release/5/egg-locations: Add optimism and schematic

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