Re: PHP 5.1

From: Date: Mon, 07 Feb 2005 20:55:24 +0000
Subject: Re: PHP 5.1
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-14701@lists.php.net to get a copy of this message
(I don't mind this discussion, but as several have asked for this subthread on overloading be ended, it may be best to do so) On Thu, 2005-02-03 at 19:58, Terje Slettebø wrote: > > From: "Stig S. Bakken" <stig@yahoo-inc.com> > > > Oh, and the comment about "Code should be readable, not cuddly-cute": > Operator overloading is about being able to express your intent clearer in > the code - leading to _more_ readable code than the corresponding function > alternative (see my arithmetic example in another posting). It has nothing > to do with cuteness, and everything to do with being to express your intent > clearly in the code. >My take on this is that you are, in many more cases than not, able to >express your intent much clearer with good method names. This is >especially true if operator overloading were not part of the language >initially. Your example with complex math is an exception. You're entitled to your opinion, of course. >Please explain how you see operator overloading making functional >programming possible/easier in PHP? By enabling the creation of function objects, which may hold state (unlike functions, unless you use "static"). One typical feature of functional programming languages is the ability to do "partial application", i.e. giving a function less than the arguments it needs, yielding a function with some of the arguments bound. To illustrate its use, I can give a "simple" example in C++: #include <iostream> class plus { public: int operator()(int a,int b) { return a+b; } }; template<class T> class apply1 { public: apply1(int v) : bound_1st(v) {} int operator()(int b) { return T()(bound_1st, b); } private: int bound_1st; }; int main() { std::cout << plus()(1,3) << "\n"; std::cout << apply1<plus>(1)(3) << "\n"; } The first line of main() creates a "plus" object ("plus()"), and calls its overloaded operator() with 1 and 3, giving 4. The second line creates an "apply1" object (taking "plus" as a type parameter, and 1 as a constructor parameter - "apply1<plus>(1)"), and then its overloaded operator() is called with 3. As the first parameter, 1, is "bound" by "apply1", the result is, once again, 4. So the above prints "4 4" (separated by newline). apply1<plus>(1) creates a function object which has bound one of plus'es parameters, which means it becomes a "1 + ?" function, effectively, or in other words, a partial application of the "plus" function (object). Naturally, you may get a similar effect in PHP by using named member functions rather than operator(). As another poster pointed out, $object() already has a (different) meaning. Regards, Terje

« previous php.internals (#14701) next »