[racket] contracts ->i optional keyword syntax

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Wed Aug 27 16:32:27 EDT 2014

#lang racket

(module x racket
  (provide
   (contract-out 
    (rename f g (-> integer? any/c))))
  
  (define f displayln))

(require 'x)

(g 'a)




On Aug 27, 2014, at 2:18 PM, Kevin Forchione <lysseus at gmail.com> wrote:

> 
> On Aug 27, 2014, at 11:12 AM, Matthias Felleisen <matthias at ccs.neu.edu> wrote:
> 
>> 
>> On Aug 27, 2014, at 12:22 PM, Kevin Forchione <lysseus at gmail.com> wrote:
>> 
>>> 
>>> #lang racket
>>> 
>>> (provide (contract-out [foo 
>>>                        (-> string? list?)])
>>>         (rename-out (foo bar)))
>>> 
>>> (define (foo n) (list n))
>>> 
>>> (module+ test
>>>  (require (submod ".."))
>>>  (foo 3)
>>>  (bar "3”))
>>> 
>>> Here foo violates the contract.  Changing the argument to foo to “3” and  the argument to bar to 3 shows that bar does not violate the contract. So the contract is bound to foo and not to the rename. Quite interesting!
>>> 
>> 
>> 
>> contract-out attaches a contract to foo so when you can call foo on 3, you get an error. 
>> 
>> provide also exports foo as bar w/o contract, so you can call it on anything you want. 
>> 
>> This is clearly what the words of the program imply, and the semantics of the language gives it to you. No problems!
> 
> Yes, I agree. Very useful! Is there a way to attach a contract to a renamed identifier? I tried wrapping contract around a rename form and it didn’t appear to like it.
> 
> -Kevin
> 



Posted on the users mailing list.