On Thu, Dec 1, 2016 at 8:59 PM, Alexis King <lexi.lam...@gmail.com> wrote:
> > On Dec 1, 2016, at 16:29, David Storrs <david.sto...@gmail.com> wrote: > > > > - This function returns #t because it is a simple test function intended > to get the hang of hash contracts... > > - This function takes one argument... > > - Which is a hash... > > - Which has keys 'success, 'file-id, 'scratchdir-path, and 'chunk-hash... > > - All of which are symbols... > > - The values will be, respectively: > > success boolean? > > file-id (or/c #f exact-positive-integer?) > > scratchdir-path path-string? > > chunk-hash string? > > The hash/dc contract is not designed to assign contracts to the values > associated with specific keys; rather, it allows the contract on a value > to depend generally on the value of the key. You could theoretically use > the right hand side of the contract to do a case analysis on the value > of the key, but that would not be pretty. This is mostly intentional, > though: Racket hashes are designed to be used as dictionaries, not > structures. > The difference between a dictionary and a structure being that dictionaries are easily extensible on the fly and structures are not? I'm curious -- what are the elements of that design and what are the reasons? It seems like a natural fit -- if Racket supported contracts on the values of a contract then you would have the best of dictionaries and structures. (I'm worried that the above sounds snarky, but I'm not sure how to rephrase it It's not intended as snark, it's an honest question.) > > It sounds like you likely want a struct, not a hash. Probably something > like this: > > (struct some-name (success file-id scratchdir-path chunk-hash)) > > …with the following contract: > > (struct/c some-name > boolean? > (or/c #f exact-positive-integer?) > path-string? > string?) > > Hm. Well, that approach would work. It's not really what I'm looking for, though -- this is data that's coming back from a SQL query and being forwarded on to another function for further processing. It isn't needed anywhere else, and creating a struct for this one use feels pretty clumsy and heavyweight. "Hash of field-name-in-table to value-in-field" seemed like a really intuitive solution. It's fine, though. I can just do a manual check. So, given that these don't work the way I thought, how DO they work? I still can't understand this documentation -- like, literally *at all*. I have not managed to write a single non-trivial contract thus far. Could you please provide some examples of how to use hash contracts and why? For example, I don't see how to do something as simple as "this hash must have the following keys". Or how to say that some keys will be of different types -- e.g., 'foo and "bar". -- > You received this message because you are subscribed to the Google Groups > "Racket Users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to racket-users+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.