Re: Code References

From: Date: Mon, 19 May 2003 15:13:56 +0000
Subject: Re: Code References
References: 1 2 3 4  Groups: php.internals 
Request: Send a blank email to internals+get-1714@lists.php.net to get a copy of this message
At 16:41 19/05/2003, Sterling Hughes wrote:
On Mon, 2003-05-19 at 10:59, Zeev Suraski wrote: At 16:23 19/05/2003, Sterling Hughes wrote:
On Mon, 2003-05-19 at 10:47, George Schlossnagle wrote:
Excuse my ignorance, but how does this differ from the features offered by create_function? (Other than having nicer aesthetics.)
create_function is runtime for one thing, this would be compile time (code reference, doesn't need to be relookedup). And then of course there are aesthetics, its incredibly ugly to do anything remotely complex with create_function.
Yikes. I really think create_function is more than enough for what we need in PHP. Whether or not there's an extra lookup for such an expensive operation (fcall) should not be the reason to add such a feature. It's a Well, its much more than a lookup, its an eval.
No it isn't. It's a lookup.
Also, I can tell you haven't used create_function that much. Even though I use closures alot (when I program Perl), I stay away from create_function() because its ugly (sorry :) $ref = create_function ('$a, $b', <<<HEREDOC \$c = \$a + \$b; \$d = \$c * \$a; return $\d; HEREDOC;); compared to: $ref = function ($a, $b) {
    $c = $a + $b;
    $d = $c + $a;
    return $d;
}
How about
$ref = create_function('$a, $b', '
    $c = $a + $b;
    $d = $c + $a;
    return $d;
');
?
And that's in the *simplest* of cases. For anything moderately complex, create_function isn't an option. (Plus you have the eval overhead at runtime, and a function lookup.) What I'm proposing is easier, and also faster.
Look, Sterling, the performance gain is going to be negligible. Contrary to your perception, it does not use eval() any more than your proposed solution would - the compiler has to be invoked to parse the code, just as much the compiler will have to parse the code in your case. In both cases, the code gets compiled just once. From a performance point of view, the difference boils down to a slightly slower lookup. From looks point of view, if you try to make it look nice, you can, very easily, as I illustrated. Using single quotes you only have to escape single quotes and backslashes, certainly not a big price to pay.
beast from another family of languages (functional languages), we need a VERY good reason to introduce it. You can do pretty stuff very easily with create_function and single quotes or heredocs. As I said to Wez, function pointers/code references are in virtually every language I know of. Anonymous functions are in Perl, Python, Ruby, LISP and possibly others.
And as I said in reply, they also exist in PHP. Zeev

« previous php.internals (#1714) next »