|Pathologically Eclectic Rubbish Lister|
[Perl 6] Method call versus function call on a data setby w-ber (Hermit)
|on May 29, 2007 at 22:47 UTC||Need Help??|
(This is not really Perl 6 specific, but it was Perl 6 syntax that made me think about the matter.)
In Perl 6, arrays are now objects, namely inheriting from Array, as in any self-respecting object-oriented language. As a trivial case example, suppose you want to grep for something in an arbitrary array. In Perl 5 style, you can do it as a function call to grep, giving the array as a parameter:
In Perl 6, one of the many ways to do this is to use the method call syntax on the array itself:
In the former, it is clear, at least to a Perl 5 programmer, that one has to have an array or at least something acting exactly like an array (implementing the same interface) as an argument to grep, since this is really what we are doing: grepping an array.
In the latter, the array acts as an object that happens to have the method grep, which, as it happens, will grep the array for us.
I'm not against object-oriented programming, which certainly has its place. I'm not against abstract data structures, which are certainly very useful. However, I find the latter, method call style mentally more taxing than the former, function call style. This is because in the general case, or even in this particular case, in order to find out what methods the data structure supports, one has to find out the exact type of the data structure. Perhaps it's an Array, but is it also a Frobnicator, and does it implement the role JackInTheBox? What will grep do with this data structure (or object), anyway? To find out for sure, one has to know where grep has been defined or overloaded.
This sounds silly, but it is what my unconscious has been giving me warnings about. Consider any "standard methods" for standard datatypes, such as push, pop, and grep for Array. What if the datatype we are using overloads any of those, but we want to be sure we always use the standard grep? Perhaps this is a non-issue, as one can always use the function call syntax to force interpreting grepping as grepping on an Array, or in some other way force the variable to be interpreted as an Array.
However, the other issue stands: For some reason, I find it much harder to (imagine) finding out which methods a datatype has instead of looking up built-in function definitions from perlfunc, and then weeding out those that won't work for this particular datatype I'm using (Array, Hash, etc.). This is because there can be potentially an infinite number of datatypes, all with their own set of methods. True, there can also be a potentially infinite list of (built-in) functions that operate on certain kinds of data, but at least it is always clear from the function signature what it expects the data to be and from the description what it does.
Maybe I just need to write more OOP code and leave the procedural and functional worlds for a while.(As a sidenote, there are many other ways in which to grep in Perl 6, such as:
The feed style doesn't suffer from this problem, but the alternative method call style does.)