[racket] dispatch on type

From: Stephan Houben (stephanh at planet.nl)
Date: Mon Jun 13 08:59:01 EDT 2011

Dear list,

I have code like this which dispatches on
the type of a homogenous vector:

(define (gl-vector->type vec)
   (cond
     ((bytes? vec) GL_UNSIGNED_BYTE)
     ((s8vector? vec) GL_BYTE)
     ((u16vector? vec) GL_UNSIGNED_SHORT)
     ((s16vector? vec) GL_SHORT)
     ((u32vector? vec) GL_UNSIGNED_INT)
     ((s32vector? vec) GL_INT)
     ((f32vector? vec) GL_FLOAT)
     ((f64vector? vec) GL_DOUBLE)))

I was wondering if there is a more efficient way to
do this, one which doesn't involve a linear scan
over all possible types. I presume that at the C level
there is a "type tag" associated with any of these types;
is it possible to access this from the Racket level?

Thanks,

Stephan


Posted on the users mailing list.