Allow omitting type annotation in most of the for: forms.

This commit is contained in:
Vincent St-Amour 2011-06-13 16:58:03 -04:00
parent ef11f754e5
commit a5bccaffe1
3 changed files with 36 additions and 18 deletions

View File

@ -0,0 +1,9 @@
#lang typed/racket
(ann (for/list ([#{i : Number} (in-list '(1 2 3))]) i) (Listof Number))
(ann (for/list: ([i : Number (in-list '(1 2 3))]) i) (Listof Number))
(+ (for/fold: ([acc : Number 0])
([i (in-list '(1 2 3))])
(+ i acc))
1)

View File

@ -133,3 +133,11 @@
(~or rest:annotated-star-rest rest:annotated-dots-rest)))
#:with ann-formals #'(n.ann-name ... . rest.ann-name)
#:with (arg-ty ...) #'(n.ty ... . rest.formal-ty)))
(define-splicing-syntax-class standalone-annotation
#:literals (:)
(pattern (~seq : t)
#:with ty #'t))
(define-splicing-syntax-class optional-standalone-annotation
(pattern (~optional a:standalone-annotation)
#:with ty (if (attribute a) #'a.ty #f)))

View File

@ -491,6 +491,11 @@ This file defines two sorts of primitives. All of them are provided into any mod
(when guard
#,(loop #'(rest ...))))])))]))
(define-for-syntax (maybe-annotate-body body ty)
(if (syntax-e ty)
(syntax-property body 'type-ascription ty)
body))
;; Handling #:when clauses manually, like we do with for: above breaks
;; the semantics of for/list and co.
;; We must leave it to the untyped versions of the macros.
@ -500,19 +505,17 @@ This file defines two sorts of primitives. All of them are provided into any mod
(define-for-syntax (define-for-variant name)
(lambda (stx)
(syntax-parse stx #:literals (:)
[(_ : ty
[(_ a:optional-standalone-annotation
(clause:for-clause ...)
c:expr ...)
(syntax-property
(maybe-annotate-body
(quasisyntax/loc stx
(#,name
(clause.expand ... ...)
#,@(syntax-property
#,@(maybe-annotate-body
#'(c ...)
'type-ascription
#'ty)))
'type-ascription
#'ty)])))
#'a.ty)))
#'a.ty)])))
(define-syntax (define-for-variants stx)
(syntax-parse stx
[(_ (name untyped-name) ...)
@ -537,17 +540,16 @@ This file defines two sorts of primitives. All of them are provided into any mod
;; clauses with these 2.
(define-syntax (for/lists: stx)
(syntax-parse stx #:literals (:)
[(_ : ty
[(_ a:optional-standalone-annotation
((var:optionally-annotated-name) ...)
(clause:for-clause ...)
c:expr ...)
(syntax-property
(maybe-annotate-body
(quasisyntax/loc stx
(for/lists (var.ann-name ...)
(clause.expand ... ...)
c ...))
'type-ascription
#'ty)]))
#'a.ty)]))
(define-syntax (for/fold: stx)
(syntax-parse stx #:literals (:)
[(_ : ty
@ -591,15 +593,14 @@ This file defines two sorts of primitives. All of them are provided into any mod
(define-for-syntax (define-for*-variant name)
(lambda (stx)
(syntax-parse stx #:literals (:)
[(_ : ty
[(_ a:optional-standalone-annotation
(clause:for-clause ...)
c:expr ...)
(syntax-property
(maybe-annotate-body
(quasisyntax/loc stx
(#,name (clause.expand ... ...)
c ...))
'type-ascription
#'ty)])))
#'a.ty)])))
(define-syntax (define-for*-variants stx)
(syntax-parse stx
[(_ (name no-*-name) ...)