Posts Tagged ‘interpreters’

Dynamic Scalar Type Hinting on PHP6

Tuesday, November 20th, 2007

I was recently following a thread on the php-dev list that intrigued me enough to write about a new syntax feature for PHP.

Sam Barrow recently posted a patch about optional scalar value type hinting that got me thinking that it was a smart approach to making php even more structured, but using a dynamic approach, since PHP has given us flexibility, let’s keep moving forward with it.

I propose a Dynamic Scalar Type Hinting syntax where we can declare data-types and our boundary checking callbacks during runtime.

We always have to do boundary checking and type checking on our functions, even though we already know the type to be used in context.

  1. <?php
  2.  
  3. function bounds_check_uint($d) {
  4. if($d < 0 && $d <= 65535)
  5. throw new Exception(‘Value out of range’);
  6.  
  7. return true;
  8. }
  9.  
  10.  
  11. function mul_uint($a1, $a2) {
  12. try {
  13. bounds_check_uint($a1);
  14. bound_check_uint($a2);
  15. } catch($e) {
  16. echo $e->getMessage();
  17. }
  18.  
  19. return $a1 * $a2;
  20. }
  21.  
  22. function sub_uint($a1, $a2) {
  23. try {
  24. bounds_check_uint($a1);
  25. bound_check_uint($a2);
  26. } catch($e) {
  27. echo $e->getMessage();
  28. }
  29.  
  30. return $a1 - $a2;
  31. }
  32.  
  33. function add_uint($a1, $a2) {
  34. try {
  35. bounds_check_uint($a1);
  36. bound_check_uint($a2);
  37. } catch($e) {
  38. echo $e->getMessage();
  39. }
  40.  
  41. return $a1 + $a2;
  42. }
  43. ?>

Let’s propose the following syntax:

  1. <?php
  2.  
  3. function bounds_check_uint($d) {
  4. if($d < 0 && $d <= 65535)
  5. throw new Exception();
  6.  
  7. return true;
  8. }
  9.  
  10. define_scalar(‘uint’, ‘bounds_check_uint’);
  11.  
  12. function mul_uint(<uint> $a1, <uint> $a2) {
  13. return $a1 * $a2;
  14. }
  15.  
  16. function sub_uint(<uint> $a1, <uint> $a2) {
  17. return $a1 - $a2;
  18. }
  19. function add_uint(<uint> $a1, <uint> $a2) {
  20. return $a1 + $a2;
  21. }
  22.  
  23. ?>

Here it appears that passing a scalar type reference via ‘<' + literal + '>‘ syntax just left adjacent from your parameter you can set an inline and possibly an internal type check.

Some might say why not use Java? This is what non-dynamic based languages such as C++, Java, C#, etc. platforms of that caliber would bring to the table.

They require that you declare your type ahead of time and don’t give you the option to define and adjust during runtime without an obscure mannerism. Dynamic Scalar Type’s do not have to be defined at all or during runtime. They can also be used as inline type hinting for runtime based performance optimizations in the execution engine. They can be used by documentation tools to better generate phpdoc’s.

When all is said and done, I think this could be a fine addition to the PHP6 or possibly future versions beyond the platform. Thanks for listening and feel free to share your comments. As I continue to work on this patch to PHP6 CVS, I’ll need a sounding board.