rfc:pow-operator

Differences

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

Link to this comparison view

rfc:pow-operator [2013/12/19 01:56]
datibbaw
rfc:pow-operator [2017/09/22 13:28]
Line 1: Line 1:
- 
-====== PHP RFC: Power Operator ====== 
-  * Version: 0.1 
-  * Date: 2013-11-23 
-  * Author: Tjerk Meesters, datibbaw@php.net 
-  * Status: Voting 
-  * First Published at: http://wiki.php.net/rfc/pow-operator 
- 
-===== Introduction ===== 
- 
-This proposal is three-fold: 
- 
-  - Turn the existing [[http://php.net/pow|pow()]] function into a language construct. 
-    * Avoids a function call. 
-    * Support for [[rfc:operator_overloading_gmp|GMP overloading]]. 
-  - Introduce an exponential (right associative) operator ''<nowiki>**</nowiki>''. 
-    * Easier to read and shorter to write. 
-    * Can be found in other languages. 
-  - Introduce an exponential assignment operator ''<nowiki>**=</nowiki>'' 
- 
-===== Proposal ===== 
- 
-**pow() as language construct** 
- 
-No changes to existing code, safe for one: 
- 
-<code php> 
-var_dump(pow([], 2)); // int(0) instead of float(0) 
-</code> 
- 
-**Power operator** 
- 
-A short working example: 
- 
-<code php> 
-echo 2 ** 3; // 8 
-</code> 
- 
-It supports [[operator_overloading_gmp|GMP overloading]]: 
- 
-<code php> 
-$base = gmp_init(2); 
-$exponent = 3; 
-var_dump($base ** $exponent); 
- 
-// output 
-object(GMP)#3 (1) { 
-  ["num"]=> 
-  string(1) "8" 
-} 
-</code> 
- 
-Example of exponent assignment: 
- 
-<code php> 
-$x = 2; 
-$x **= 3; 
-echo $x; // 8 
-</code> 
- 
-**Important** 
- 
-The proposed associativity is right, just like how power towers work. 
- 
-The operator precedence is: 
-  * higher than the bitwise not (~), 
-  * lower than array dereferencing, 
-  * lower than unary minus. 
- 
-Examples: 
- 
-<code php> 
-echo 2 ** 3 ** 2; // 512 (not 64) 
-echo -3 ** 2; // 9 (not -9) 
-echo ~3 ** 2; // -10 (not 16) 
-</code> 
- 
-===== Discussion ===== 
- 
-> Should ''2 <nowiki>**</nowiki> 3 <nowiki>**</nowiki> 2'' yield ''64'' (left associative), ''512'' (right associative) or throw an error (non associative)? 
- 
-The exponent operator evaluation order should be based on [[http://en.wikipedia.org/wiki/Tetration|Tetration]] and therefore be right associative.  
- 
-**Languages with left associative exponential operator** 
- 
-  * VB (not by choice imho) 
-  * Basic 
-  * Octave 
-  * Matlab 
-  * D 
-  * ColdFusion 
- 
-** Languages with right associative exponential operator** 
- 
-  * Haskell 
-  * R 
-  * F# 
-  * Ruby 
-  * Perl 
-  * Python 
-  * Mathematica 
-  * Freemat 
-  * Scilab 
-  * Tcl (changed from left associative!) 
-  * Cobol 
-  * Fortran 
-  * Sage 
-  * Bash 
- 
-**Languages with non associative exponential operator** 
- 
-  * Ada 
- 
-**Sources** 
-  * https://plus.google.com/u/0/104277466162910953762/posts/e3jCt51VfmD 
-  * http://perldoc.perl.org/perlop.html 
-  * http://www.tcl.tk/cgi-bin/tct/tip/274.html 
-  * http://book.realworldhaskell.org/read/getting-started.html 
-  * http://www.nku.edu/~foxr/CSC407/NOTES/ch7.ppt‎ (second slide) 
- 
-===== Proposed PHP Version(s) ===== 
- 
-PHP 5.6 
- 
-===== Impact to Existing Extensions ===== 
- 
-The opcode ''ZEND_POW <165>'' and ''ZEND_ASSIGN_POW <166>'' is added. 
- 
-External extensions such as [[http://pecl.php.net/package/vld|vld]] or [[https://github.com/krakjoe/phpdbg|phpdbg]] would have to be updated, but I'm not aware of any core extensions that would otherwise be affected. 
- 
-===== Open Issues ===== 
- 
-None. 
- 
- 
-===== Proposed Voting Choices ===== 
- 
-Voting will be based on the following: 
- 
-  * Turn ''pow()'' into a language construct, 
-  * Add the power operators ''<nowiki>**</nowiki>'' and ''<nowiki>**</nowiki>='', 
-  * Add ''ZEND_POW'' and ''ZEND_ASSIGN_POW'' opcodes. 
- 
----- 
- 
-<doodle title="Should PHP get a power operator in PHP 5.6?" auth="datibbaw" voteType="single" closed="false"> 
-   * Yes 
-   * No 
-</doodle> 
- 
-Voting ends on 31st of December. 
- 
----- 
-===== Patches and Tests ===== 
- 
-Power operator PR: https://github.com/php/php-src/pull/543 
  
rfc/pow-operator.txt · Last modified: 2017/09/22 13:28 (external edit)