rfc:typecheckingweak

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revisionBoth sides next revision
rfc:typecheckingweak [2009/07/12 08:28] – Initial work zeevrfc:typecheckingweak [2009/07/12 09:37] – More initial work (conversion logic) zeev
Line 27: Line 27:
  
  
-===== Suggested Solution =====+===== Proposed Solution =====
  
 Conceptually, user functions will be able to denote that they are expecting a specific type of scalar value, using syntax similar to that of class type hinting.  This notation will be optional;  If absent - the existing behavior will continue. Conceptually, user functions will be able to denote that they are expecting a specific type of scalar value, using syntax similar to that of class type hinting.  This notation will be optional;  If absent - the existing behavior will continue.
  
 +<code php>
 function foo(int x) {} function foo(int x) {}
 function bar(x, y, float z) {} function bar(x, y, float z) {}
 function baz(int x, float y, string z) {} function baz(int x, float y, string z) {}
 +function foobar(int &x) {}
 +</code>
  
 Once a function argument has been designated a scalar type hint - the function author is completely relieved of any further checks and conversions, and is assured that his or hers code will always be supplied with an argument of the designated type. Once a function argument has been designated a scalar type hint - the function author is completely relieved of any further checks and conversions, and is assured that his or hers code will always be supplied with an argument of the designated type.
Line 40: Line 43:
  
   - Does the value to-be-passed have the type required by the function code?  If so, pass it on as-is.  If not - move to step 2.   - Does the value to-be-passed have the type required by the function code?  If so, pass it on as-is.  If not - move to step 2.
-  - Can the value be converted to the type required by the function (as per the conversion table below)?  If so - convert and pass it on.  If not - move to step 3.+  - Can the value be converted to the type required by the function (as per the Conversion Logic below)?  If so - convert and pass it on.  If not - move to step 3.
   - Emit an error or throw an exception.   - Emit an error or throw an exception.
  
 +**Note:**  In step 2, if the argument is designated as a pass-by-reference argument - the conversion will apply to the variable being passed.  This is consistent with the expectation that arguments passed by reference may be modified by the function they're sent to.
  
  
 +===== Conversion Logic =====
 +
 +^ value                   ^ string       ^ float      ^ int       ^ numeric   ^ bool      ^
 +^ true (boolean)          | <font color="red">fail</font>     | 1.0        | 1         | 1         | //as-is// |
 +^ false (boolean)         | //fail//     | 0.0        | 0         | 0         | //as-is// |
 +^ 0 (integer)             | '0'          | 0.0        | //as-is// | //as-is// | false     |
 +^ 1 (integer)             | '1'          | 1.0        | //as-is// | //as-is// | true      |
 +^ 12 (integer)            | '12'         | 12.0       | //as-is// | //as-is// | true      |
 +^ 12.0 (double)           | '12.0'       | //as-is//  | 12        | //as-is// | true      |
 +^ 12.34 (double)          | '12.34'      | //as-is//  | 12        | //as-is// | true      |
 +^ 'true' (string)         | //as-is//    | //fail//   | //fail//  | //fail//  | //fail//  |
 +^ 'false' (string)        | //as-is//    | //fail//   | //fail//  | //fail//  | //fail//  |
 +^ '0' (string)            | //as-is//    | 0.0        | 0         | 0         | false     |
 +^ '1' (string)            | //as-is//    | 1.0        | 1         | 1         | true      |
 +^ '12' (string)           | //as-is//    | 12.0       | 12        | 12        | true      |
 +^ '12abc' (string)        | //as-is//    | //fail//   | //fail//  | //fail//  | //fail//  |
 +^ '12.0' (string)         | //as-is//    | 12.0       | 12        | 12.0      | true      |
 +^ '12.34' (string)        | //as-is//    | 12.34      | 12        | 12.34     | true      |
 +^ 'foo' (string)          | //as-is//    | //fail//   | //fail//  | //fail//  | //fail//  |
 +^ empty string (TBD)      | //as-is//    | //fail//   | //fail//  | //fail//  | //fail//  |
 +^ array () (array)        | //fail//     | //fail//   | //fail//  | //fail//  | //fail//  |
 +^ array (0 => 12) (array) | //fail//     | //fail//   | //fail//  | //fail//  | //fail//  |
 +^ NULL (NULL)             | empty string | 0.0        | 0         | 0         | false     |
 +
 +
 +//fail//  - designates failure, either emitting an error or throwing an exception
 +
 +//as-is// - designates that the value is passed as-is, without conversion
  
  
rfc/typecheckingweak.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1