Skip to content

Commit

Permalink
Add static contracts for instanceof
Browse files Browse the repository at this point in the history
  • Loading branch information
takikawa committed Feb 26, 2014
1 parent efbd02c commit 1700597
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Expand Up @@ -373,6 +373,8 @@
(recursive-sc-use (if (from-typed? typed-side) typed-n* untyped-n*)))])]
[(Instance: (? Mu? t))
(t->sc (make-Instance (resolve-once t)))]
[(Instance: (? Name? t))
(instanceof/sc (t->sc t))]
[(Instance: (Class: _ _ fields methods _ _))
(match-define (list (list field-names field-types) ...) fields)
(match-define (list (list public-names public-types) ...) methods)
Expand Down
Expand Up @@ -14,7 +14,8 @@
(contract-out
[struct member-spec ([modifier symbol?] [id symbol?] [sc static-contract?])]
[object/sc ((listof object-member-spec?) . -> . static-contract?)]
[class/sc ((listof member-spec?) boolean? (listof identifier?) (listof identifier?) . -> . static-contract?)]))
[class/sc ((listof member-spec?) boolean? (listof identifier?) (listof identifier?) . -> . static-contract?)]
[instanceof/sc (static-contract? . -> . static-contract?)]))



Expand Down Expand Up @@ -55,6 +56,27 @@
(define (sc->constraints v f)
(merge-restricts* 'impersonator (map f (member-seq->list (combinator-args v)))))])

(struct instanceof-combinator combinator (class)

This comment has been minimized.

Copy link
@endobson

endobson Feb 28, 2014

Since class is a static contract it should be part of the args field so that it gets printed. Currently I believe that it won't get printed at all and all instanceof-combinators will look the same.

#:transparent
#:property prop:combinator-name "instanceof/sc"
#:methods gen:sc
[(define (sc-map v f)
(match v
[(instanceof-combinator args class)
;; FIXME: is this variance correct?

This comment has been minimized.

Copy link
@endobson

endobson Feb 28, 2014

This looks right to me. The class should protect the same as the instance (with regards to positive/negative parties).

(instanceof-combinator args (f class 'covariant))]))
(define (sc-traverse v f)
(match v
[(instanceof-combinator _ class)
(f class 'covariant)
(void)]))
(define (sc->contract v f)
(instance/sc->contract v f))
(define (sc->constraints v f)
(match v
[(instanceof-combinator _ class)
(f class)]))])


(define member-seq->list
(match-lambda
Expand All @@ -81,6 +103,8 @@
(object-combinator (member-seq specs)))
(define (class/sc specs opaque absent-fields absent-methods)
(class-combinator (member-seq specs) opaque absent-fields absent-methods))
(define (instanceof/sc class)
(instanceof-combinator null class))

(define (wrap mod ctc)
(define mod-stx
Expand Down Expand Up @@ -115,3 +139,7 @@
#`(class/c #,@(if opaque (list '#:opaque) empty)
#,@(map (member-spec->form f) vals)
(absent #,@absent-methods (field #,@absent-fields)))]))
(define (instance/sc->contract v f)
(match v
[(instanceof-combinator _ class)
#`(instanceof/c #,(f class))]))

0 comments on commit 1700597

Please sign in to comment.