(Houdini VEX) - any way to template value types for general functions?

I’m hitting some frustrations trying to work with attribute data in custom functions - I also have limited understanding of C-style templating, but it seems like a limitation with how VEX is parsed.

In my case, I want to get an array of all the attribute values of points in geometry - here just “P”. But this attribute could be anything, and therefore could return an array of any type.

wrangle code:


#define ptattrs(vT)\
    function vT[] ptattrs_(int geo; string atname){ \
        int nels = npoints(geo); \
        vT vals[]; \
        resize(vals, nels); \
        for(int i = 0; i < nels; i++){ \
            append(vals, point(geo, atname, i)); \
        }\
        return vals; \
    }

vector poses[] = ptattrs(vector)(0, "P");
    

I think the issue here is that I’m trying to declare and then call a function in the same line, which is just invalid syntax once it’s all expanded.

Is there a better way to format/structure this in a library? This hopefully won’t be the only example, so manually typing out variants of the function for every vex value type is not appealing.

Thanks

Yep, moving the macro call and the function call to separate lines solves it

#define ptattrs(vT)\
    function vT[] ptattrs_(int geo; string atname){ \
        int nels = npoints(geo); \
        vT vals[]; \
        resize(vals, nels); \
        for(int i = 0; i < nels; i++){ \
            append(vals, vT(point(geo, atname, i))); \
        }\
        return vals; \
    }

ptattrs(vector)
vector poses[] = ptattrs_(0, "P");

so it seems you have to declare general functions in this templated style, then run them all through each value type that you need