[plt-scheme] no args with case-lambda

From: David J. Neu (djneu at att.net)
Date: Fri Feb 6 10:54:43 EST 2004

Hi all,

I have a function that I'd like to accept, 0, 1 or 2 arguments.

Is there a way to have case-lambda match the 0 args case explicitly or
must I match to a list, check if the list is null? and then raise an
exn:application:arity if it is?

I show an example below where I raise the exception.

Many thanks for any help!

--David


(require (lib "date.ss" "mzlib"))

; (current-date-time) => 2004-02-06 09:39:40
; (current-date-time #f) => 2004-02-06
; (current-date-time #t 'american) => Friday, February 6th, 2004 9:39:40am
(define current-date-time
  (let ((cdt (lambda (atime aformat)
               (let ((ld #f))
                 (date-display-format aformat)
                 (set! ld (date->string (seconds->date (current-seconds)) atime))
                 (date-display-format 'american)
                 ld))))
    (case-lambda
      ((ltime) (cdt ltime 'iso-8601))
      ((ltime lformat) (cdt ltime lformat))
      ;(lls (cdt #t 'iso-8601)))))      
      ; Q: what if lls is not '()
      (lls (if (null? lls)
                 (cdt #t 'iso-8601)
                 (let ((llen (length lls)))
                   (raise (make-exn:application:arity 
                           (format "current-date-time: expects 0,1 or 2 arguments, given ~a" llen)
                           (current-continuation-marks)
                           llen
                           0))))))))



(display (current-date-time))
(newline)
(display (current-date-time #f))
(newline)
(display (current-date-time #t 'american))
(newline)
(with-handlers
    ((exn:application:arity?
      (lambda (e) (printf "got ~a args but wanted ~a!~n" (exn:application-value e) (exn:application:arity-expected e)))))
  (display (current-date-time #t 'american 'junk)))
(newline)
(display (current-date-time #t 'american 'junk))


Posted on the users mailing list.