[racket] Contracts in interfaces

From: Asumu Takikawa (asumu at ccs.neu.edu)
Date: Tue Sep 25 01:43:39 EDT 2012

On 2012-09-24 22:00:29 -0700, Gregory Woodhouse wrote:
>    In writing contracts for classes I always use the ->m constructor for
>    methods, so this example of section 5.1 of the Reference surprises me
>
>    (define file-interface<%>
>      (interface () open close read-byte write-byte))
>    (define directory-interface<%>
>      (interface (file-interface<%>)
>        [file-list (-> (listof (is-a?/c file-interface<%>)))]
>        parent-directory))
>
>    It looks like -> contract constructor means the same thing except, of
>    course, the context is an interface definition and a class contract, so
>    it's clear from context that this a method and not a function.

The `->` there is a typo and it should actually be `->m`. I'll fix the
docs for the next release.

Note that you *can* use `->` for method contracts though, in which case
you need to specify an extra argument corresponding to the `this`
argument implicitly passed to methods.

>    Second question: Is the inclusion of contracts in interface definitions a
>    new feature? I've always written interfaces using only method names and
>    have missed the ability to specify the shape of the method (something more
>    analogous to what a Java interface does). It's entirely possible that this
>    has been in the Reference a long time and I just missed it. In fact, I'm
>    continually amazed at how much is there that I've missed.

Yes, it's a new feature that was included in Racket 5.3. Interface
contracts are complementary to `class/c` contracts and work slightly
differently. Most importantly, any class that implements an interface
(either directly or indirectly) with a contracted method will also have
a contract on that method.

Cheers,
Asumu

Posted on the users mailing list.