Enable prefab support correctly for define-struct

I had forgotten to adjust the define-struct macro to work
like the struct macro for the #:prefab keyword, which made
TR think prefabs were ordinary structs.

Closes GH issue #188
This commit is contained in:
Asumu Takikawa 2015-08-21 16:38:04 -04:00
parent 51cd8db3d6
commit 20f3badc98
2 changed files with 20 additions and 2 deletions

View File

@ -128,13 +128,15 @@
[(_ vars:maybe-type-vars nm:struct-name (fs:fld-spec ...)
opts:struct-options)
(let ([mutable? (if (attribute opts.mutable?) #'(#:mutable) #'())]
[cname (second (build-struct-names #'nm.name null #t #t))])
[cname (second (build-struct-names #'nm.name null #t #t))]
[prefab? (if (attribute opts.prefab?) #'(#:prefab) #'())])
(with-syntax ([d-s (ignore-some
(syntax/loc stx (define-struct nm (fs.fld ...) . opts)))]
[dtsi (quasisyntax/loc stx
(dtsi* (vars.vars ...) nm (fs.form ...)
#:maker #,cname
#,@mutable?))])
#,@mutable?
#,@prefab?))])
#'(begin d-s dtsi)))]))
(lambda (stx)
(syntax-parse stx

View File

@ -4,6 +4,8 @@
(struct foo ([x : Symbol]) #:prefab)
(struct bar foo ([y : String] [z : String]) #:prefab)
(define-struct foo* ([x : Symbol]) #:prefab)
(define-struct (bar* foo*) ([y : String] [z : String]) #:prefab)
(: a-bar (Prefab (bar foo 1) Symbol String String))
(define a-bar (bar 'foo "bar1" "bar2"))
@ -11,6 +13,9 @@
(foo-x (foo 'foo))
(bar-y (bar 'foo "bar1" "bar2"))
(foo*-x (make-foo* 'foo))
(bar*-y (make-bar* 'foo "bar1" "bar2"))
;; prefab keys may be normalized or not
(: a-bar-2 (Prefab (bar 2 foo 1 (0 #f) #()) Symbol String String))
(define a-bar-2 (bar 'foo "bar1" "bar2"))
@ -22,18 +27,29 @@
;; Mutable prefab structs
(struct baz ([x : String]) #:mutable #:prefab)
(define-struct baz* ([x : String]) #:mutable #:prefab)
(define a-baz (baz "baz"))
(set-baz-x! a-baz "baz2")
(baz-x a-baz)
(define a-baz* (make-baz* "baz"))
(set-baz*-x! a-baz* "baz2")
(baz*-x a-baz*)
;; Polymorphic prefab structs
(struct (X) poly ([x : X]) #:prefab)
(define-struct (X) poly* ([x : X]) #:prefab)
(poly-x (poly "foo"))
(poly-x (poly 3))
(poly-x #s(poly "foo"))
(poly*-x (make-poly* "foo"))
(poly*-x (make-poly* 3))
(poly*-x #s(poly* "foo"))
;; Test match (indirectly tests unsafe-struct-ref)
(match (foo 'x) [(foo s) s])
(match (foo* 'x) [(foo* s) s])