On Jun 21, 2015, at 5:41 AM, Aidan Gauland <aidal...@slingshot.co.nz> wrote:

> Say I want to define a flat contract for the "freshness" field of my
> fish class, so I define a contract for it like so:
> 
> (define/contract fish-freshness/c
>  (-> any/c
>      boolean?)
>  (flat-named-contract 'fish-freshness/c
>    (lambda (x)
>      (not (eq? #f (member x '(fresh frozen edible rotten)))))))
> 
> Am I barking up the wrong tree here, or is this perfectly sensible?
> 
> Regards,
> Aidan Gauland

If this does what you want, it’s fine, but the define/contract is probably 
unnecessary, and
(define fish-freshness/c
  (flat-named-contract ‘fish-freshness/c
    (lambda (x)
      (not (eq? ….)))))
Should do what you want.

The flat-named-contract is also probably unnecessary (though it can be useful 
if the name isn’t just a symbol), so
(define (fish-freshness/c x)
  (not (eq? ….)))
Will also do what you want.

And finally, another way of doing this would be:
(define fish-freshness/c (or/c ‘fresh ‘frozen ‘edible ‘rotten))

Or if you prefer giving the contract a better name for error messages:
(define fish-freshness/c
  (flat-named-contract ‘fish-freshness/c (or/c ‘fresh ‘frozen ‘edible ‘rotten)))

Any one of these is “sane” as long as they work, and it depends on which one 
you think looks more readable, makes you’re intent clear, and gives error 
messages that make sense.

I just finished this when I saw Matthias Felleisen’s reply.  I’m sending it 
anyway.


-- 
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.

Reply via email to