[racket] JSExprs and hashtables

From: Jordan Johnson (jmj at fellowhuman.com)
Date: Mon Jan 19 12:29:43 EST 2015

Great! Thanks for the discussion.

Best,
Jordan

> On Jan 19, 2015, at 5:41 AM, Alexander D. Knauth <alexander at knauth.org> wrote:
> 
> I managed to fix this for your example by inserting these 3 lines into typed-racket/types/remove-intersect.rkt after line 67:
>          [(list-no-order (Value: (not (? hash?)))
>                          (or (? Hashtable?) (? HashtableTop?)))
>           #f]
> There is probably a better solution though.
> 
>> On Jan 19, 2015, at 7:36 AM, Alexander D. Knauth <alexander at knauth.org> wrote:
>> 
>> 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
>> 
>> ____________________
>>  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/64fd1d2b/attachment.html>

Posted on the users mailing list.