README.md (3006B)
1 [](https://travis-ci.org/jsmaniac/type-expander) 2 [](https://coveralls.io/github/jsmaniac/type-expander) 3 [](http://jsmaniac.github.io/travis-stats/#jsmaniac/type-expander) 4 [](http://docs.racket-lang.org/type-expander/) 5 [](https://github.com/jsmaniac/type-expander/issues) 6 7 Type-expander 8 ============= 9 10 This project is written for 11 [Typed/Racket](https://docs.racket-lang.org/ts-guide/) using Literate 12 Programming. See the “[Implementation of the type expander 13 library](http://docs.racket-lang.org/type-expander/)” part of the [online 14 documentation](http://docs.racket-lang.org/type-expander/) if you want to dig 15 into the source. 16 17 This library enhances typed/racket with type expanders, which are special 18 macros that can appear where a type would normally be expected, and must 19 expand to a type. Type expanders are to types what match expanders are to 20 match patterns. It is based on Asumu Takikawa's [type 21 expanders](https://github.com/racket/racket/compare/master...takikawa:tr-type-expander) 22 (see also his [original pull request 23 here](https://github.com/racket/racket/pull/604)). Asumu Takikawa's work 24 attempted to integrate type expanders directly into Typed/Racket. This 25 project instead implements type expanders as a library and works without any 26 modification of the core Typed/Racket codebase. This shows the extensibility 27 of Typed/Racket thanks to macros, and could serve as the basis for other 28 projects which need to alter the manner in which Typed/Racket handles types. 29 30 Installation 31 ============ 32 33 ``` 34 raco pkg install --deps search-auto type-expander 35 ``` 36 37 Usage example 38 ============= 39 40 The `type-expander` is enabled by simply requiring the `type-expander` module 41 in a `typed/racket` program. 42 43 #lang typed/racket 44 (require type-expander) 45 46 For example, one can write the `(HomogeneousList n t)` type-expander, which 47 expands to the type for a list of `n` elements of type `t`: 48 49 (require (for-syntax syntax/parse racket/list)) 50 (define-type-expander (HomogeneousList stx) 51 (syntax-parse stx 52 [(_ t:expr n:nat) 53 #`(List #,@(map (λ (x) #'t) 54 (range (syntax-e #'n))))])) 55 56 It can then be used wherever a regular type is usually expected: 57 58 (: five-strings (→ String (HomogeneousList String 5))) 59 (define (five-strings x) 60 (list x "a" "b" "c" "d")) 61 62 (five-strings "hello") 63 ;; => '("hello" "a" "b" "c" "d") 64 65 (ann (five-strings "moon") (HomogeneousList String 5)) 66 ;; => '("moon" "a" "b" "c" "d")