[racket] Contract for function with vararg

From: Alexander D. Knauth (alexander at knauth.org)
Date: Sun Jun 29 13:08:06 EDT 2014

You can use or/c to do this, as long as you guard them like this so that no more than one contract matches at a time:
(or/c
 (and/c (procedure-has-arity/c 2) (-> input-port? any/c any))
 (and/c (procedure-has-arity/c 3) (-> input-port? any/c any/c any))
 (and/c (procedure-has-arity/c 4) (-> input-port? any/c any/c any/c any))
 (->* (input-port? any/c) #:rest list? any)
 )

Or, you could use if/c from unstable/contract, or define a cond/c macro like this:
(require unstable/contract)
(define-syntax cond/c
  (syntax-rules (else)
    [(cond/c) none/c]
    [(cond/c [else else-c])
     else-c]
    [(cond/c [pred then-c] clause ...)
     (if/c pred
           then-c
           (cond/c clause ...))]))

(cond/c [(procedure-has-arity/c 2) (-> input-port? any/c any)]
        [(procedure-has-arity/c 3) (-> input-port? any/c any/c any)]
        [(procedure-has-arity/c 4) (-> input-port? any/c any/c any/c any)]
        [else (->* (input-port? any/c) #:rest list? any)])

By the way it has to be procedure-has-arity/c, not procedure-includes-arity/c:
(define (procedure-has-arity/c arity)
  (define normalized-arity (normalize-arity arity))
  (flat-named-contract
   `(procedure-has-arity/c ,normalized-arity)
   (λ (f)
     (and (procedure? f)
          (arity=? (procedure-arity f) normalized-arity)))))

On Jun 29, 2014, at 8:00 AM, Roman Klochkov <kalimehtar at mail.ru> wrote:

> How to make a contract, that accepts 
> (-> input-port? any/c any)
> (-> input-port? any/c any/c any)
> (-> input-port? any/c any/c any/c any)
> ...
> and
> (->* (input-port? any/c) #:rest list? any)
> 
> 
> So theare should be at least two args and first arg should be input-port.
> 
> -- 
> Roman Klochkov
> 
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20140629/223fb772/attachment.html>

Posted on the users mailing list.