[racket] JSExprs and hashtables
Since hash-tables can be mutable as far as TR knows, (HashTable Symbol String) is not a subtype of (HashTable Symbol JSExpr).
To work around this, you can do this:
#lang typed/racket/base
(require typed/json)
(: jsx JSExpr) ; <-- jsx is what I ultimately want.
(define jsx (ann #hasheq((a . "val1") (b . "val2") (c . "val3")) (HashTable Symbol JSExpr)))
Or this would work too:
#lang typed/racket/base
(require typed/json)
(: jsx (HashTable Symbol JSExpr)) ; <-- jsx is what I ultimately want.
(define jsx #hasheq((a . "val1") (b . "val2") (c . "val3")))
Or you could use (inst make-immutable-hasheq Symbol JSExpr) instead of using #hasheq, and it would do the same thing.
On Jan 19, 2015, at 1:28 AM, Jordan Johnson <jmj at fellowhuman.com> wrote:
> Hi all,
>
> I’ve been trying to create JSExprs, as defined in the typed/json library. This is one of those instances where it seems like I must be making things harder than they should be, but I’m still feeling like a duck on rollerskates in TR, so I want to make sure I’m not missing an easier way to do it.
>
> Would you please confirm if this is indeed a smallest-possible solution?
>
> ;;;; begin
> #lang typed/racket/base
>
> (require typed/json)
>
> (: jsx JSExpr) ; <-- jsx is what I ultimately want.
>
> ;; Attempt #1:
> #;
> (define jsx #hasheq((a . "val1") (b . "val2") (c . "val3")))
>
> #| Resulting error:
> Type Checker: type mismatch
> expected: JSExpr
> given: (HashTable Symbol String)
>
> [This seems weird, since it seems like (HashTable Symbol String) should be
> a subtype of JSExpr. Is the problem that the typechecker can’t recognize
> the Strings as JSExprs?]
> |#
>
> ;; Attempt #N (shortest successful version I’ve found):
> (define jsx
> (for/hasheq : (HashTable Symbol JSExpr) ([k '(a b c)]
> [v '("val1" "val2" "val3")])
> (values k v)))
> ;;;; end
>
> Regards,
> Jordan
>
> ____________________
> 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/20150119/782b4714/attachment.html>