Re: Full proposal for the localized error message API
Lassi Kortela 26 Jul 2020 22:01 UTC
Here's a sample implementation and example:
(define-record-type foreign-error
(make-foreign-error* message)
foreign-error?
(message foreign-error:message*))
(define (make-foreign-error alist)
(make-foreign-error*
(let ((pair (assoc 'message alist)))
(and pair (cdr pair)))))
(define foreign-error:message
(case-lambda
((ferr)
(foreign-error:message ferr #f))
((ferr locale)
(let ((message (foreign-error:message* ferr)))
(cond ((procedure? message) (message locale))
((string? message) (and (not locale) message))
(else #f))))))
(define (foreign-error:locales ferr)
(let ((message (foreign-error:message* ferr)))
(cond ((procedure? message) (message #t))
((string? message) '(#f))
(else '()))))
(define (disp . xs) (for-each display xs) (newline))
(define (display-ferr ferr)
(let ((locales (foreign-error:locales ferr)))
(disp locales)
(for-each (lambda (locale)
(disp locale " => " (foreign-error:message ferr locale)))
locales)
(newline)))
(display-ferr
(make-foreign-error
'((message . #f))))
(display-ferr
(make-foreign-error
'((message . "Foo bar"))))
(display-ferr
(make-foreign-error
`((message . ,(lambda (locale)
(case locale
((#t) '(#f en-US fi-FI))
((#f en-US) "Foo")
((fi-FI) "Bar")
(else #f)))))))