title: how to use structure member types for hashing?
body: <p>I am porting a hashing code for <a href="https://en.wikipedia.org/wiki/External_Data_Representation" rel="nofollow noreferrer">XDR</a> communication, so It needs to hash the data type to send it. Types should be uint8 sint8, uint16, sint16, ... , sint64</p>
  
  <pre><code>;; lisp types -- byte := bit
  (deftype uint8  () '(UNSIGNED-BYTE 8))
  (deftype sint8  () '(SIGNED-BYTE 8))    ;
  (deftype uint16 () '(UNSIGNED-BYTE 16))
  (deftype sint16 () '(SIGNED-BYTE 16))
  (deftype uint32 () '(UNSIGNED-BYTE 32))
  (deftype sint32 () '(SIGNED-BYTE 32))
  (deftype uint64 () '(UNSIGNED-BYTE 64))
  (deftype sint64 () '(SIGNED-BYTE 64))
  </code></pre>
  
  <p>"I am using SBCL"</p>
  
  <p>The data is stored in a structure with types specified</p>
  
  <p>Problems:</p>
  
  <ul>
  <li>When the data is 0  or 1 it gets type bit which fits in all -fixnum, unsigned-byte, signed-byte- types.</li>
  <li>Types size inference!! for signed and unsigned</li>
  <li>Types use type hierarchy, so I can't have unique types.</li>
  </ul>
  
  <p>I only use types for hashing the structure with (<code>etypecase</code>, <code>subtypep</code>)</p>
  
  <p>I have read <a href="https://www.semanticscholar.org/paper/Xdr-Binding-to-Lisp-Antoy-Davidson/1492cfc3b024ebbfaf30007d1512e7c697958640" rel="nofollow noreferrer">Xdr-Binding-to-Lisp</a>, but I don't understand how they used <code>case</code> or <code>typecase</code> to differ between uint8 and sint8. <a href="https://github.com/fjames86/drx" rel="nofollow noreferrer">drx on github</a> uses encode_int, encode_array, ...</p>
  
  <p>What I want:</p>
  
  <pre><code>(defstruct example
    (x 0           :type lcm:sint32)
    (y "blablabla" :type string)
    (z 1.8d0       :type double-float))
  </code></pre>
  
  <p>quickly hash the structure slots information using (<code>etypecase</code>, <code>subtypep</code>).</p>
  
  <p>EDIT:</p>
  
  <p>I want to hash lisp-structure <code>TYPE SLOTS</code> "string sint8 float array" to read the data correctly when received/sent. If I have a struct. st:</p>
  
  <pre><code>(typecase st
      (structure-object (recursive do this))
      (sint8 (do this))
      (string (recursive do this))
      (uint8 ( do this))
  ...
  </code></pre>
  
  <p>I use typecase to recursively convert type info. ---> integer</p>
  
  <p>The following is the code for structure definition:</p>
  
  <pre><code>(defvar *struct-types* (make-array 0
                                     :adjustable t
                                     :fill-pointer 0))
  
  (defmacro deflcmstruct (name options &amp;rest slots)
    "Define a struct"
    (let ((slot-types '()))
      `(progn
         (defstruct (,name (:include lcm-type options))
           ,@(mapcar (lambda (slot)
                       (destructuring-bind (slot-name slot-type &amp;rest slot-args)
                           slot
                         ()
                         (declare (ignore slot-type))
                         `(,slot-name ,@slot-args)))
                     slots))
         ,(vector-push-extend  *struct-types* name)
         ,(vector-push-extend  *struct-types* (make-hash-table))
  </code></pre>
  
  <p>Questions:</p>
  
  <ul>
  <li>If a lisp structure has a slot with number say: 5 how to make lisp understand 5 type uniquely as sint8/uint8/sin16/uint16, but not fit all these types only one?</li>
  <li>How can I hash data types(custom) using lisp?</li>
  <li>If I have to store the type def. then What are options to do so?? I am think of building a hash-table using <code>gensym</code>, but this hash-table would be floating around!!</li>
  <li>How to make data-type structure isolated?or better a better design?</li>
  <li>Examples would be great.</li>
  </ul>
  
  <p>THANKS IN ADVANCE</p>