From d35fb82f4a583a6b27164ecd92fd8d52c0dd72db Mon Sep 17 00:00:00 2001 From: Asumu Takikawa Date: Thu, 20 Feb 2014 14:13:41 -0500 Subject: [PATCH] Make TR's `for/hash` form more flexible --- .../typed-racket-lib/typed-racket/base-env/prims.rkt | 9 +++++++-- .../tests/typed-racket/unit-tests/typecheck-tests.rkt | 8 ++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/pkgs/typed-racket-pkgs/typed-racket-lib/typed-racket/base-env/prims.rkt b/pkgs/typed-racket-pkgs/typed-racket-lib/typed-racket/base-env/prims.rkt index 623fe09346..4d97f4490b 100644 --- a/pkgs/typed-racket-pkgs/typed-racket-lib/typed-racket/base-env/prims.rkt +++ b/pkgs/typed-racket-pkgs/typed-racket-lib/typed-racket/base-env/prims.rkt @@ -1086,7 +1086,7 @@ This file defines two sorts of primitives. All of them are provided into any mod (lambda (stx) (syntax-parse stx #:literals (:) - ((_ (~seq : return-annotation:expr) + [(_ (~seq : return-annotation:expr) clause:for-clauses body ...) ; body is not always an expression, can be a break-clause (quasisyntax/loc stx @@ -1094,7 +1094,11 @@ This file defines two sorts of primitives. All of them are provided into any mod ((return-hash : return-annotation (ann (#,hash-maker null) return-annotation))) (clause.expand ... ...) (let-values (((key val) (let () body ...))) - (hash-set return-hash key val)))))))) + (hash-set return-hash key val))))] + [(_ clause:for-clauses body ...) + (syntax/loc stx + (for/hash (clause.expand ... ...) + body ...))]))) (define-syntax for/hash: (define-for/hash:-variant #'make-immutable-hash)) (define-syntax for/hasheq: (define-for/hash:-variant #'make-immutable-hasheq)) @@ -1148,6 +1152,7 @@ This file defines two sorts of primitives. All of them are provided into any mod (quasisyntax/loc stx (#,l/c k.ann-name . body))])) (values (mk #'let/cc) (mk #'let/ec)))) + ;; lambda with optional type annotations, uses syntax properties (define-syntax (-lambda stx) (syntax-parse stx diff --git a/pkgs/typed-racket-pkgs/typed-racket-test/tests/typed-racket/unit-tests/typecheck-tests.rkt b/pkgs/typed-racket-pkgs/typed-racket-test/tests/typed-racket/unit-tests/typecheck-tests.rkt index 587b2902d1..56fcc25237 100644 --- a/pkgs/typed-racket-pkgs/typed-racket-test/tests/typed-racket/unit-tests/typecheck-tests.rkt +++ b/pkgs/typed-racket-pkgs/typed-racket-test/tests/typed-racket/unit-tests/typecheck-tests.rkt @@ -2039,6 +2039,14 @@ (foo #hash(("foo" . foo)))) (-HT -Symbol -String)] + ;; for/hash doesn't always need a return annotation inside + [tc-e (let () + (tr:define h : (HashTable Any Any) + (for/hash ([(k v) (in-hash #hash(("a" . a)))]) + (values v k))) + h) + (-HT Univ Univ)] + ;; call-with-input-string and friends - PR 14050 [tc-e (call-with-input-string "abcd" (lambda: ([input : Input-Port]) (values 'a 'b))) #:ret (ret (list (-val 'a) (-val 'b)))]