I defined a function true? for use with count in racket/list.
(define (true? expr)
(and (boolean? expr) expr #t))
I noticed I could provide it numeric arguments and my function would happily return #f.
> (true? 6)
#f
So, I thought I would explore using a racket contract to make non-boolean arguments return an error in contract violation. So I put this code at the tope of my file:
(provide (contract-out
[true? (-> boolean? boolean?)]))
However, after adding the contract I still get the same behavior as above in the racket REPL. I don’t understand how that could be. What am I missing?
Contracts are usually enforced between modules. So you would have to try it from the outside perspective. The REPL, however, applies from inside the module you’re working in.
An easy way to test from the outside is to use a test submodule. For example:
Change the contracts and re-run in DrRacket, and you should see your contracts in effect here, since the
testmodule here is being treated as an external customer of the contract.Alternatively, make another file that
requires the first, and then you can see the effect of contracts there too. If the first file is calledtrue-test.rkt, then you can make another module, and then: