In Common Lisp I have often wanted to iterate through the fields of a struct-- to comb out references to a deleted object, for example, or find fields that are uninitialized. I know the structs are just vectors underneath. And yet I can't write a general purpose function that I can call on any struct. I can only access the fields by name, because that's what a struct is supposed to mean.Now, in Perl 5 the contents of anything can be inspected and iterated. There are two kinds of collections: arrays and hashes. Either can be iterated, so objects using a collection for a list of instance data can be iterated. Why do this? Think Data::Dumper. It's handy for debugging and exploring, not to mention persistance. And his examples of combing out references to something deleted or checking for undef's isn't a bad idea in Perl, either.
Obviously, it's possible to write non-iterating objects in Perl 5, since you can choose any implementation you like. The instance data can be a blessed scalar cookie, and the real data visible only to the access methods.
Now Perl 6, from what I've heard, is exactly this way. The object's state is stored in a manner known only to the implementation, and all access is really done though access method function calls.
How can you Dump that?
Even if you must call the approved functions to get the values, simply having a list of what those functions are would be handy. That is, a list of members available as meta-data.
This might be possible as a result of other features. For example, scan the package's symbol table for subs, and figure out which are methods (leading dot in the name? associated properties? How to tell the difference between a simple accessor and more complex functions?). I think the designers should make sure this is possible. Perhaps publishing a list of functions that were generated as accessors to named instance data is the only for-sure solution to enable something like Data::Dumper to be general-purpose.
—John
In reply to Make sure Perl 6 is introspective enough by John M. Dlugosz
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |