[plt-scheme] serialization: transient fields
> ----------------------------------------
>
> (module transient mzscheme
> (require (lib "struct.ss")
> (lib "serialize.ss")
> (lib "class.ss"))
>
> (provide define/transient)
>
> (define-serializable-class* transient% object% (externalizable<%>)
> (init-field [val #f])
> (define/public (externalize) #f)
> (define/public (internalize v) (void))
> (super-new))
>
> (define transient-val (class-field-accessor transient% val))
> (define set-transient-val! (class-field-mutator transient% val))
>
> (define-syntax define/transient
> (syntax-rules ()
> [(_ id v)
> (begin
> (define t-id (new transient% [val v]))
> (define-syntax id
> (syntax-id-rules (set!)
> [(set! _ val)
> (set-transient-val! t-id val)]
> [(_ arg (... ...))
> ((transient-val t-id) arg (... ...))]
> [_
> (transient-val t-id)])))])))
>
>
I quickly realized this doesnt work with deserialization. The exact
problem, if it isn't obvious, seems to be the class body is never
evaluated and so any constructors or definitions are not called when the
object is deserialized. Maybe thats the wrong conclusion to come to but
that seems to be the case. I tried changing the transient definition so
that the internalize method would always create the transient class with
the proper value, that value passed as v to (define t-id v).
(define-serializable-class* transient% object% (externalizable<%>)
(init-field [val #f] [create-thunk (lambda () (void))])
(define/public (externalize) #f)
(define/public (internalize v) (create-thunk))
(super-new))
(define transient-val (class-field-accessor transient% val))
(define set-transient-val! (class-field-mutator transient% val))
(define-syntax define/transient
(syntax-rules ()
[(_ id v)
(begin
(define t-id (new transient% [val v] [create-thunk (lambda () v)]))
(define-syntax id
(syntax-id-rules (set!)
[(set! _ val)
(set-transient-val! t-id val)]
[(_ arg (... ...))
((transient-val t-id) arg (... ...))]
[_
(transient-val t-id)])))]))
But it always seems as though (create-thunk) returns (void) no matter
what. I guess my original conclusion identifies why this is the case but
for some reason I though it would work anyway. Any ideas?