[racket] typed racket and generic interfaces, is there a workaround using properties?

From: Alexander D. Knauth (alexander at knauth.org)
Date: Sat Sep 27 13:55:12 EDT 2014

Actually using prop:dict works (I hadn’t found prop:dict yet when I first asked this)
#lang typed/racket

(define-type Dict-Ref ([Dict Any] [Any] . ->* . Any))
(define-type Dict-Set! (Dict Any Any . -> . Void))
(define-type Dict-Set (Dict Any Any . -> . Dict))
(define-type Dict-Remove! (Dict Any . -> . Void))
(define-type Dict-Remove (Dict Any . -> . Dict))
(define-type Dict-Iterate-First (Dict . -> . Any))
(define-type Dict-Iterate-Next (Dict Any . -> . Any))
(define-type Dict-Iterate-Key (Dict Any . -> . Any))
(define-type Dict-Iterate-Value (Dict Any . -> . Any))
(define-type Dict-Count (Dict . -> . Natural))

(require/typed racket/dict
               [#:opaque Dict dict?]
               [dict-ref Dict-Ref]
               [dict-set! Dict-Set!]
               [dict-set Dict-Set]
               [dict-remove! Dict-Remove!]
               [dict-remove Dict-Remove]
               [dict-iterate-first Dict-Iterate-First]
               [dict-iterate-next Dict-Iterate-Next]
               [dict-iterate-key Dict-Iterate-Key]
               [dict-iterate-value Dict-Iterate-Value]
               [dict-count Dict-Count]
               [prop:dict Struct-Type-Property])

(define (make-dict-prop #:dict-ref [dict-ref : Dict-Ref]
                        #:dict-set! [dict-set! : (U Dict-Set! #f) #f]
                        #:dict-set [dict-set : (U Dict-Set #f) #f]
                        #:dict-remove! [dict-remove! : (U Dict-Remove! #f) #f]
                        #:dict-remove [dict-remove : (U Dict-Remove #f) #f]
                        #:dict-count [dict-count : Dict-Count]
                        #:dict-iterate-first [dict-iterate-first : Dict-Iterate-First]
                        #:dict-iterate-next [dict-iterate-next : Dict-Iterate-Next]
                        #:dict-iterate-key [dict-iterate-key : Dict-Iterate-Key]
                        #:dict-iterate-value [dict-iterate-value : Dict-Iterate-Value])
  (vector-immutable dict-ref
                    dict-set!
                    dict-set
                    dict-remove!
                    dict-remove
                    dict-count
                    dict-iterate-first
                    dict-iterate-next
                    dict-iterate-key
                    dict-iterate-value))

(struct foo () #:transparent
  #:property prop:dict
  (make-dict-prop #:dict-ref (lambda (this key [failure #f]) "whatever")
                  #:dict-count (lambda (this) 0)
                  #:dict-iterate-first (lambda (this) #f)
                  #:dict-iterate-next (lambda (this pos) 0)
                  #:dict-iterate-key (lambda (this pos) pos)
                  #:dict-iterate-value (lambda (this pos) "whatever")))

(dict-ref (assert (foo) dict?) "idontkare")


On Sep 27, 2014, at 1:07 AM, Spencer florence <spencerflorence at gmail.com> wrote:

> I don’t think you can. You would need define the struct in an untyped module then require it via require/typed. This is why dict’s don’t work in typed/racket either.
> 
> 
> 
> On Sat, May 24, 2014 at 9:39 PM, Alexander D. Knauth <alexander at knauth.org> wrote:
> 
> Do generic interfaces work using structure type properties, and if they do, is there a way to use generic interfaces through properties so that I can do it in typed racket? 
> 
> Specifically I’m trying to use the gen:custom-write and gen:dict generic interfaces. I can use prop:custom-write for the first one, but I don’t know what to do for gen:dict. 
> 
> Otherwise I’ll just put it in an untyped submodule. 
> 
> 
> ____________________ 
> Racket Users list: 
> http://lists.racket-lang.org/users 
> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20140927/17fc9f08/attachment.html>

Posted on the users mailing list.