[racket] dispatch on type

From: Eli Barzilay (eli at barzilay.org)
Date: Mon Jun 13 16:40:36 EDT 2011

8 hours ago, Stephan Houben wrote:
> 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?

If this is only for such vectors, you might consider using cvectors
instead.

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                    http://barzilay.org/                   Maze is Life!


Posted on the users mailing list.