Hacker News new | past | comments | ask | show | jobs | submit login

If you have a hierarchy of types

    struct A {vector<B> Children; int NChildren = 2, ...};
    struct B {vector<C> Children; int NChildren = 2; ...};
    struct C {vector<D> Children; int NChildren = 2; ...};
    struct D {...)
and a function template

    template<typename T> T miniParser() { return T{}; }
and you call it like

    struct A;
    recurseFillChildren<A, B, C, D>(A);
It expands into nested loops that fill the "Children" areas of each type with two default-constructed values of the next type down the hierarchy, until you get to D, at which point it does nothing.

The reason you can't implement this in Rust is that Rust Generics (their "template-y" language feature) is missing a few capabilities relative to C++ templates. Namely,

1. They don't operate on numbers

2. They have no notion of collections of types

Because of this, they

1. Can't know that there is a collection of types

2. Can't extract the size of the collection of types

This comes up in numerical code, where in C++ it is easy to write e.g. geometry code that is generic in the number of spatial dimensions, but in Rust it is a pain in the ass.




Consider applying for YC's W25 batch! Applications are open till Nov 12.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: