pike

List all members of a class in Pike


In Pike, it is possible to retrieve all members of an object by calling indices(). Is it also possible to see all members of a class without instantiating it?

> class A {int foo; string bar;};
> A a = A();
> indices(a);
(1) Result: ({ /* 2 elements */
                "foo",
                "bar"
            })
> indices(A);
(2) Result: ({ })

Solution

  • Yes, you can, although output won't be as friendly as indices one. You need to use _describe_program function, like this:

    > _describe_program(A);
    (4) Result: ({ /* 2 elements */
                ({ /* 7 elements */
                    0,
                    "foo",
                    int,
                    0,
                    0,
                    0,
                    0
                }),
                ({ /* 7 elements */
                    0,
                    "bar",
                    string,
                    0,
                    16,
                    0,
                    0
                })
            })