Re: Contracts question on structs

David Storrs <[email protected]>
Newsgroups gmane.comp.lang.racket.user,gmane.lisp.scheme.plt
Message-ID <CAE8gKoct8ZXQrSEFCgz0QnhixdE4pAwvsC+NMYXWEbgBf+UvHw@mail.gmail.com>
On Fri, Feb 15, 2019 at 12:18 AM David Storrs <[email protected]> wrote:
>
> On Thu, Feb 14, 2019 at 9:08 PM Robby Findler
> <[email protected]> wrote:
> >
> > This is what struct/dc is for. Let me know if the docs let you down!
> >
> > Robby
>
> This is good to hear about, because I'm actually in the middle of
> writing something that it will help with.  Thanks, Robby.
>
> One question:  Is there a typo in the example code or am I missing something?
>
> (struct bt (val left right))
> (define (bst/c lo hi)
> (or/c #f
> (struct/dc bt
> [val (between/c lo hi)]
> [left (val) #:lazy (bst lo val)]                   ; should that be (bt lo val)?
> [right (val) #:lazy (bst val hi)])))             ; and this be (bt val hi) ?

Ugh.  I hate it when I make significant typos in code questions.  I
should have asked if it was supposed to (bst/c lo val) and (bst/c val
hi). (i.e., is there a missing /c in the code?)  If not, where is bst
defined?

>
> Also, am I correct that the contract is not actually attached to the
> bt structure at this point?  That's a thing that would be done
> elsewhere, probably in the provide statement.  Yes?
>
> >
> > On Thu, Feb 14, 2019 at 7:57 PM Chris GauthierDickey <[email protected]> wrote:
> >>
> >> I'm wondering if it's possible to have a contract for a struct that only allows certain kinds of initializations. For example, I have this:
> >>
> >> (provide (contract-out
> >>           [struct Result ((name (or/c Temp? Label?)) (global? boolean?) (value (or/c VarValue? #f)))]))
> >>
> >> But it's too general. What I'd really like to have is a contract that combines them like an or/c (I can see what this would cause problems, just wondering if there's a clever way around it):
> >> (provide (contract-out
> >>           [or/c (struct Result ((name Temp?) (global? #f) (value (or/c VarValue? #f))))
> >>                 (struct Result ((name Label?) (global? boolean?) (value (or/c (VarValue #f)))))]))
> >>
> >> In this case, if name is Temp? then global? has to be #f. If name is Label? then global? can be #t or #f. I'd like to use struct cause it provides all the contracts for accessors and such. Note, I can use a #:guard on the struct, but wondered if it was possible to do something along these lines with a contract.
> >>
> >> Thanks!
> >> Chris
> >>
> >>
> >> --
> >> 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+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
> >> 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+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
> > 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+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
For more options, visit https://groups.google.com/d/optout.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.