Fix hash.refs example. Fixes #15.
This commit is contained in:
parent
2af1256427
commit
27fc80861a
42
index.rkt
42
index.rkt
|
@ -1177,17 +1177,16 @@ like JavaScript?
|
||||||
(require (for-syntax racket/syntax))
|
(require (for-syntax racket/syntax))
|
||||||
(define-syntax (hash.refs stx)
|
(define-syntax (hash.refs stx)
|
||||||
(syntax-case stx ()
|
(syntax-case stx ()
|
||||||
;; If the optional `default' is missing, assume it's #f.
|
;; If the optional `default' is missing, use #f.
|
||||||
[(_ chain)
|
[(_ chain)
|
||||||
#'(hash.refs chain #f)]
|
#'(hash.refs chain #f)]
|
||||||
[(_ chain default)
|
[(_ chain default)
|
||||||
(let ([xs (map (lambda (x)
|
(let* ([chain-str (symbol->string (syntax->datum #'chain))]
|
||||||
(datum->syntax stx (string->symbol x)))
|
[ids (for/list ([str (in-list (regexp-split #rx"\\." chain-str))])
|
||||||
(regexp-split #rx"\\."
|
(format-id #'chain "~a" str))])
|
||||||
(symbol->string (syntax->datum #'chain))))])
|
(with-syntax ([hash-table (car ids)]
|
||||||
(with-syntax ([h (car xs)]
|
[keys (cdr ids)])
|
||||||
[ks (cdr xs)])
|
#'(hash-refs hash-table 'keys default)))]))
|
||||||
#'(hash-refs h 'ks default)))]))
|
|
||||||
;; Gives us "sugar" to say this:
|
;; Gives us "sugar" to say this:
|
||||||
(hash.refs js.a.b.c)
|
(hash.refs js.a.b.c)
|
||||||
;; Try finding a key that doesn't exist:
|
;; Try finding a key that doesn't exist:
|
||||||
|
@ -1208,26 +1207,23 @@ messages when used in error. Let's try to do that here.
|
||||||
(syntax-case stx ()
|
(syntax-case stx ()
|
||||||
;; Check for no args at all
|
;; Check for no args at all
|
||||||
[(_)
|
[(_)
|
||||||
(raise-syntax-error #f "Expected (hash.key0[.key1 ...] [default])"
|
(raise-syntax-error #f "Expected hash.key0[.key1 ...] [default]" stx #'chain)]
|
||||||
stx #'chain)]
|
;; If the optional `default' is missing, use #f.
|
||||||
[(_ chain)
|
[(_ chain)
|
||||||
#'(hash.refs chain #f)]
|
#'(hash.refs chain #f)]
|
||||||
[(_ chain default)
|
[(_ chain default)
|
||||||
;; Check that chain is a symbol, not e.g. a number or string
|
(raise-syntax-error #f "Expected hash.key0[.key1 ...] [default]" stx #'chain)
|
||||||
(unless (symbol? (syntax-e #'chain))
|
(let* ([chain-str (symbol->string (syntax->datum #'chain))]
|
||||||
(raise-syntax-error #f "Expected (hash.key0[.key1 ...] [default])"
|
[ids (for/list ([str (in-list (regexp-split #rx"\\." chain-str))])
|
||||||
stx #'chain))
|
(format-id #'chain "~a" str))])
|
||||||
(let ([xs (map (lambda (x)
|
|
||||||
(datum->syntax stx (string->symbol x)))
|
|
||||||
(regexp-split #rx"\\."
|
|
||||||
(symbol->string (syntax->datum #'chain))))])
|
|
||||||
;; Check that we have at least hash.key
|
;; Check that we have at least hash.key
|
||||||
(unless (and (>= (length xs) 2)
|
(unless (and (>= (length ids) 2)
|
||||||
(not (eq? (syntax-e (cadr xs)) '||)))
|
(not (eq? (syntax-e (cadr ids)) '||)))
|
||||||
(raise-syntax-error #f "Expected hash.key" stx #'chain))
|
(raise-syntax-error #f "Expected hash.key" stx #'chain))
|
||||||
(with-syntax ([h (car xs)]
|
(with-syntax ([hash-table (car ids)]
|
||||||
[ks (cdr xs)])
|
[keys (cdr ids)])
|
||||||
#'(hash-refs h 'ks default)))]))
|
#'(hash-refs hash-table 'keys default)))]))
|
||||||
|
|
||||||
;; See if we catch each of the misuses
|
;; See if we catch each of the misuses
|
||||||
(hash.refs)
|
(hash.refs)
|
||||||
(hash.refs 0)
|
(hash.refs 0)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user