I have some polymorphic struct types defined in untyped Racket that I'd
like to be able to work with from Typed Racket. I see that struct type
variables only work with `unsafe-require/typed`; I'm trying to understand
just what unsafety I would be signing myself up for if I were to use that
so I can decide whether to try it or whether I need to use these structs
only from untyped Racket.

(I can't really move the definitions of the structs into TR because they
use properties and other things TR doesn't support.)

With some experimentation, the only unsafety I've discovered is that Typed
Racket doesn't protect me from badly-behaved constructors or accessors,
e.g. this attempts to do `(add1 "This is bad.")`:
#lang typed/racket #:no-optimize

(module untyped racket
  (provide (struct-out bad))
  (struct bad (v)
    #:guard (λ (x y) "This is bad.")
    #:transparent))

(require typed/racket/unsafe)

(unsafe-require/typed
 'untyped
 [#:struct (A) bad ([v : A])])

(add1 (bad-v (bad 12)))

If that is indeed the only unsafety, I can pretty confidently make sure
that my untyped struct definition isn't doing anything so bad as that.
Everything else I've tried has worked as I would hope, e.g. this program
raises a nice contract violation:
#lang racket

(module untyped racket
  (provide (struct-out wraper))
  (struct wraper (v)
    #:transparent))

(module typed typed/racket
  (require typed/racket/unsafe)
  (provide add-wraped)
  (unsafe-require/typed
   (submod ".." untyped)
   [#:struct (A) wraper ([v : A])])
  (: add-wraped (-> (wraper Integer)
                    (wraper Integer)
                    (wraper Integer)))
  (define (add-wraped a b)
    (wraper (+ (wraper-v a)
               (wraper-v b)))))

(require 'untyped
         'typed)

(add-wraped (wraper 1) (wraper "Not a number"))

However, I wanted to ask, because I'd rather not find out there's some
unsafety I haven't anticipated by getting a mysterious segfault.

-Philip

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

Reply via email to