Re: [racket-users] Defining contracts sanely

2015-06-21 Thread Matthias Felleisen

#lang racket

(module server racket
  (provide
   (contract-out
(fish%
 (class/c
  (update (-m degrees-of-freshness any/c))
  (field [freshness degrees-of-freshness])

  (define degrees-of-freshness
(one-of/c 'fresh 'frozen 'edible 'rotten))
  
  (define fish%
(class object%
  (super-new)
  (field [freshness 'fresh])

  ;; set this object's freshness field to v
  (define/public (update v) (set! freshness v)

(module client racket
  (require (submod .. server))

  (define fish (new fish%))

  (send fish update 'edible)

  ;; blow up if exn raised isn't contract failure 
  (with-handlers ([exn:fail:contract? void])
(send fish update 'done)
(raise 'catastrophe)))

(require 'client)



On Jun 21, 2015, at 5:41 AM, Aidan Gauland 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
 
 -- 
 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.


Re: [racket-users] Defining contracts sanely

2015-06-21 Thread Alexander D. Knauth

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.


[racket-users] Defining contracts sanely

2015-06-21 Thread Aidan Gauland
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

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