[racket] require/typed to a struct type

From: Joe Gibbs Politz (joe at cs.brown.edu)
Date: Sat Feb 9 14:25:24 EST 2013

I suppose I expected it to work the same way define-type does, where
later names are fair game to use.  I can say:

#lang typed/racket

(require/typed "untyped-struct.rkt"
 [number-me (MyNumbers -> Number)])

(define-type MyNumbers (U String Number))

(number-me 42)

But I can't do that with a struct defined later.  It was pretty
jarring and took some serious thinking to realize that I needed to
move a *require* statement to fix my code when it broke because I
decided to put a struct type in the require line.  So I was wondering
if there was a compelling engineering reason for it to be that way;
sounds like there's somewhat of a justification.

I can't move all of my require/typed declarations to the end of my TR
files, either, since the declarations are needed before certain uses
of the bindings I bring in.  Is there a recommended rule of thumb for
where to put require/typed declarations?


On Fri, Feb 8, 2013 at 11:38 PM, Eric Dobson <eric.n.dobson at gmail.com> wrote:
> Its the same reason that this program gives an error.
>
> #lang racket
>
> (define/contract (f x)
>   (my-struct? . -> . number?)
>   1)
>
> (struct my-struct ())
>
> (f (my-struct))
>
> Do you expect this to work?
>
> We could replace uses of my-struct? with (lambda (x) (my-struct? x)) but I
> wonder if the optimizer will see through that or not (Given that it actually
> is delaying the resolution of the variable). Also that doesn't return true
> to struct-predicate-procedure? and something might depend on that for
> optimizations.
>
>
> On Fri, Feb 8, 2013 at 8:20 PM, Joe Gibbs Politz <joe at cs.brown.edu> wrote:
>>
>> When using require/typed with a struct type in the signature, it seems
>> to matter if the require comes before or after the struct definition:
>>
>> typed.rkt
>> =========
>> #lang typed/racket
>>
>> (require/typed "untyped.rkt"
>>  [number-me (my-struct -> Number)])
>>
>> (struct: my-struct ())
>>
>> (number-me (my-struct))
>>
>> untyped.rkt
>> =========
>> #lang racket/base
>>
>> (provide number-me)
>> (define (number-me _) 52)
>>
>>
>> Running typed.rkt yields:
>>
>> my-struct?: undefined;
>>  cannot reference an identifier before its definition
>>
>> But, if I change typed.rkt to
>>
>> typed2.rkt
>> =========
>> #lang typed/racket
>>
>> (struct: my-struct ())
>>
>> (require/typed "untyped.rkt"
>>  [number-me (my-struct -> Number)])
>>
>> (number-me (my-struct))
>>
>>
>> There's no error and the call succeeds.
>>
>> Any reason for the ordering dependency?  It's quite inconvenient to be
>> shuffling require statements around as soon as you want to put a
>> struct in their type, so I'm assuming this wasn't intended.
>> ____________________
>>   Racket Users list:
>>   http://lists.racket-lang.org/users
>
>

Posted on the users mailing list.