[racket] Arity check

From: Gustavo Massaccesi (gustavo at oma.org.ar)
Date: Wed Aug 21 09:24:32 EDT 2013

The arity check is one of the few things I still miss from Visual
Studio. 99% of the time the functions have simple definitions and the
arity error is a mistake.

But enabling this by default can break very dynamic code, for example:

#lang racket

(define-syntax-rule (app/rep proc val)
  (cond [(= (procedure-arity proc) 1) (proc val)]
        [(= (procedure-arity proc) 2) (proc val val)]
        [else (void)]))

(app/rep cons '(?))
(app/rep car '(?))

I don't have a real life example.

IMHO these checks can be useful as an optional feature like the
additional debug and errortrace support in DrRacket.

Gustavo


On Wed, Aug 21, 2013 at 9:27 AM, Matthias Felleisen
<matthias at ccs.neu.edu> wrote:
>
> In general, the problem is undecidable:
>
> (define (f x) x)
> (define (g x y) x)
>
> (define (h x) (if (today-is-tuesday?) (h x 10) (h x)))
>
> (h f)
>
> Error?
>
> In specific circumstances, the compiler knows which function is called and
> could perform a check. By tradition, we don't but it's worth considering.
> Still, even then:
>
> (define (f x) (if (today-s-date-satisfies-fermats-last-theorem?) (car x 10)
> (car x)))
>
> Is this an error or a warning?
>
>
>
>
>
>
>
> On Aug 21, 2013, at 4:16 AM, Roman Klochkov wrote:
>
> Why racket doesn't check arity when compiling?
>
> (define (test)
>   (car 12 3))
>
> compiles and even runs. Ir raises an error only when test is called.
>
> I can have desirable result making
>
> (define-syntax car*
>   (make-set!-transformer
>    (λ (stx)
>      (syntax-case stx ()
>        [(car* x) (syntax-protect (syntax/loc stx (car x)))]
>        [(car* x ...) (raise-argument-error 'car "one argument"
> (syntax->datum #'(x ...)))]
>        [car* (syntax-protect (syntax/loc stx car))]))))
>
> (define (test)
>   (car* 12 3))
>
> But it is cumbersome (I'll have to wrap every function) and I don't
> understand why compiler shouldn't see function contracts.
>
> --
> Roman Klochkov
>
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users
>
>
>
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users
>


Posted on the users mailing list.