typed-racket/typed-racket-test/succeed/pr390-variation-3.rkt
Ben Greenman fae58e140d add types for Immutable-HashTable, Mutable-HashTable, Weak-HashTable (#559)
The old 'HashTable' type is now the union of the other 3 hash types.

- all operations that used to work on 'HashTable's still work,
  but some now have more specific outputs
- `#hash` literals have type `ImmutableHash`
- `immutable?` and `hash-weak?` are filters
- `Mutable-` and `Weak-` hashes have corresponding `Top` types, `HashTableTop` is now a union
- the contact for `(U (Immutable-Hash K1 V1) (Mutable-Hash K2 V2))` is ONE `hash/c`

Minor notes:

- renamed internal identifiers containing 'Hashtable' to all use 'HashTable'
- add Racket guide/reference 'secref' functions
2017-06-26 18:00:19 -04:00

36 lines
969 B
Racket

#lang typed/racket/base
;; Test filters for hash types
(: filter-HT (-> (HashTable Integer Integer) HashTableTop))
(define (filter-HT h)
(cond
[(immutable? h)
(ann h (Immutable-HashTable Integer Integer))]
[(hash-weak? h)
(ann h (Weak-HashTable Integer Integer))]
[else
(ann h (Mutable-HashTable Integer Integer))]))
(: filter-U (-> (U #f (HashTable Integer Integer)) Any))
(define (filter-U maybe-ht)
(cond
[(immutable? maybe-ht)
(ann maybe-ht (Immutable-HashTable Integer Integer))]
[(and maybe-ht (hash-weak? maybe-ht))
(ann maybe-ht Weak-HashTableTop)]
[(hash? maybe-ht)
(ann maybe-ht Mutable-HashTableTop)]
[else
(ann maybe-ht #f)]))
(void
(filter-HT (make-immutable-hash '((1 . 1))))
(filter-HT (make-hash '((1 . 1))))
(filter-HT (make-weak-hash '((1 . 1))))
(filter-U #f)
(filter-U (make-immutable-hash '((1 . 1))))
(filter-U (make-hash '((1 . 1))))
(filter-U (make-weak-hash '((1 . 1)))))