rfc:autoboxing

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:autoboxing [2010/05/05 16:23] – Reflect the posts on php.internals to the discussions section. moriyoshirfc:autoboxing [2017/09/22 13:28] (current) – external edit 127.0.0.1
Line 3: Line 3:
   * Date: 2010-05-04   * Date: 2010-05-04
   * Author: Moriyoshi Koizumi <moriyoshi@php.net>   * Author: Moriyoshi Koizumi <moriyoshi@php.net>
-  * Status: Under Discussion+  * Status: Inactive
   * First Published at: http://wiki.php.net/rfc/autoboxing   * First Published at: http://wiki.php.net/rfc/autoboxing
-  * Other formats .. 
  
 ===== Introduction ===== ===== Introduction =====
Line 13: Line 12:
 ===== Why do we find it useful in PHP? ===== ===== Why do we find it useful in PHP? =====
  
-In PHP, unlike other scripting languages, there are fundamental differences between primitive types and classes.   While they may contribute to a substantial boost of overall runtime performance, the differences prevent primitive values from having methods to operate with.  For example, PHP has many functions named array_xxx() that operate on an array to produce a result in an immutable manner (doesn't modify the original data to store the result).  They often look pretty much unintuitive when the operations are chained, because they don't allow one to write those operations in the order they occur, but to write them in a nested function calls where the innermost function gets called first.+In PHP, unlike other scripting languages, there are fundamental differences between primitive types and classes.   While they may contribute to a substantial boost of overall runtime performance, the differences prevent primitive values from having methods to operate with.  For example, PHP has many functions named array_xxx() that operate on an array to produce a result in an immutable manner (i.e. don't modify the original data to store the result).  They often look pretty much unintuitive when the operations are chained, because they don't allow one to write those operations in the order they occur, but to write them in a nested function calls where the innermost function gets called first.  With autoboxing, you should be able to write such a chain like the following: 
 + 
 +<code php> 
 +<?php 
 +$sum = $arr->keys()->sum(); // with autoboxing 
 +$sum = array_sum(array_keys($arr)); // without autoboxing 
 +?> 
 +</code>
  
 ===== Proposal ===== ===== Proposal =====
Line 26: Line 32:
 </code> </code>
  
-To enable autoboxing on integer value, one could write+To enable autoboxing on integer values, one could write:
  
 <code php> <code php>
Line 73: Line 79:
  
 ===== Discussions ===== ===== Discussions =====
-==== Exception on infeasible conversion ==== 
-[[http://news.php.net/php.internals/48204|Pas wrote]]: 
-> It could throw an exception. 
- 
-==== Relation to SPL Type library ==== 
-[[http://news.php.net/php.internals/48196|Daniel Egeberg wrote]]: 
-> Is there any reason why primitives couldn't be autoboxed to SplInt, SplBool, etc.((http://php.net/manual/book.spl-types.php))? These classes could maybe even be extended with method aliases to the relevant functions in PHP's library. 
- 
-==== Efficiency ==== 
-[[http://news.php.net/php.internals/48209|Dmitry Stogov wrote]]: 
-> I am afraid, this magic method will make php slower even if scripts don't use this future (at least the patch   disables code specialization for ZEND_INIT_METHOD_CALL) and make some future type propagation optimizations non-applicable.  
- 
 ==== Conflicts between libraries that utilizes this feature ==== ==== Conflicts between libraries that utilizes this feature ====
 [[http://news.php.net/php.internals/48195|Cornelious wrote]]: [[http://news.php.net/php.internals/48195|Cornelious wrote]]:
Line 96: Line 90:
  
 === Possible fix === === Possible fix ===
-  * Per-namespace autoboxing rules (like extension methods in C#) +  * Per-namespace autoboxing rules (like extension methods in C#) -- The idea is to allow namespaces to have each ''%%__autobox()%%'' magic method and limiting the scope where it takes effect to the namespace in which it is declared or used through ''use'' statement
-    The idea is to allow namespaces to have each ''%%__autobox()%%'' magic method and limiting the scope where it takes effect to the namespace in which it is declared or used through ''use'' statement.+ 
 +==== Relation to the existing PECL libraries ====  
 +[[http://news.php.net/php.internals/48196|Daniel Egeberg wrote]]: 
 +> Is there any reason why primitives couldn't be autoboxed to SplInt, SplBool, etc.((http://php.net/manual/book.spl-types.php))? These classes could maybe even be extended with method aliases to the relevant functions in PHP's library.
  
-==== Good place is runkit ==== 
 [[http://news.php.net/php.internals/48202|Brian Moon wrote]]: [[http://news.php.net/php.internals/48202|Brian Moon wrote]]:
 > I liken this to pecl/runkit. "For all those things you.... probably  shouldn't have been doing anyway". It will create a world where scripts are not portable. And if you need that for your internal project, that is fine. But, having this as part of the PHP core would be a disaster. This is even more heinous than %%__autoload()%%, IMO. SPL fixed this for autoload. I would support an SPL extenstion to treat primitive types as SPL objects. They are standardized. Not random. > I liken this to pecl/runkit. "For all those things you.... probably  shouldn't have been doing anyway". It will create a world where scripts are not portable. And if you need that for your internal project, that is fine. But, having this as part of the PHP core would be a disaster. This is even more heinous than %%__autoload()%%, IMO. SPL fixed this for autoload. I would support an SPL extenstion to treat primitive types as SPL objects. They are standardized. Not random.
  
- +==== Efficiency ==== 
-==== Context information to the callback ==== +[[http://news.php.net/php.internals/48209|Dmitry Stogov wrote]]: 
-[[http://news.php.net/php.internals/48199|Benjamin Eberlei wrote]]: +I am afraidthis magic method will make php slower even if scripts don't use this future (at least the patch   disables code specialization for ZEND_INIT_METHOD_CALL) and make some future type propagation optimizations non-applicable
-Should'nt any autobox callback should not only recieve the value to be autoboxedbut also the context information? I.e. the method name to be called on the variable? otherwise you cannot decide between different behaviours.+
  
 ==== Magic functions considered harmful ==== ==== Magic functions considered harmful ====
Line 113: Line 108:
 ]]))). ]]))).
  
 +==== Context information to the callback ====
 +[[http://news.php.net/php.internals/48199|Benjamin Eberlei wrote]]:
 +> Should'nt any autobox callback should not only recieve the value to be autoboxed, but also the context information? I.e. the method name to be called on the variable? otherwise you cannot decide between different behaviours.
 +
 +==== Exception on infeasible conversion ====
 +[[http://news.php.net/php.internals/48204|Pas wrote]]:
 +> It could throw an exception.
  
 ===== Patch ===== ===== Patch =====
 A preliminary patch (may be a bit outdated) is available at http://gist.github.com/162517 . A preliminary patch (may be a bit outdated) is available at http://gist.github.com/162517 .
  
-===== Changelog =====+===== Discussion on the List =====
  
 +  - 2005-02-09 : [[http://markmail.org/message/2bnwe77ie3yhc65i|Similar discussion back in 2005]]  
 +  - 2010-05-03 : [[http://markmail.org/message/a3df3nt7hyn4qsne|Initial discussion about this RFC]]
 +
 +===== Changelog =====
 2010-05-04: initial version 2010-05-04: initial version
 +
rfc/autoboxing.1273076610.txt.gz · Last modified: 2017/09/22 13:28 (external edit)