[plt-scheme] serialization: transient fields

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Wed Aug 9 08:31:19 EDT 2006

At Tue, 08 Aug 2006 22:37:38 -0400, Jon Rafkind wrote:
> I am using serialization on a class defined by define-serializable-class
> and want some members to not be serialized, equivalent to the
> `transient' keyword in Java.

Below is an implementation of `define/transient'.

Matthew

----------------------------------------

(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%))
         (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)])))])))



Posted on the users mailing list.