Req #68126 [Com]: syntax for optional definition of function return type
| From: | edgar dot r dot sandi at gmail dot com | Date: | Wed, 25 Feb 2015 01:07:17 +0000 |
| Subject: | Req #68126 [Com]: syntax for optional definition of function return type | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-190939@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=68126&edit=1
ID: 68126
Comment by: edgar dot r dot sandi at gmail dot com
Reported by: lefevre dot 10 at osu dot edu
Summary: syntax for optional definition of function return
type
Status: Open
Type: Feature/Change Request
Package: *General Issues
PHP Version: 7.0
Block user comment: N
Private report: N
New Comment:
Hi
this feature has been implemented in PHP 7
https://wiki.php.net/rfc/return_types
Edgar R. Sandi
cheers
Previous Comments:
------------------------------------------------------------------------
[2014-10-01 19:43:56] requinix@php.net
Keep an eye on https://wiki.php.net/rfc/returntypehinting
------------------------------------------------------------------------
[2014-10-01 19:13:24] lefevre dot 10 at osu dot edu
Description:
------------
It would be useful to optionally be able define a return type for a function, perhaps like so:
function foo($arg1, $arg2) returns array { ... }
Currently the Code by Contract principle is not fully implementable in interfaces because there is
no way to guarantee a return type for functions, or even any return value at all.
Adding a return type would allow one to do better code by contract:
interface iMyInterface {
function handle_data($arg1) returns iMyResponse;
}
interface iMyResponse { ... }
When writing code that implements or depends on iMyInterface, I can know what type I will be getting
out of the handle_data method.
Optionally one could specify multiple return types:
function my_string_function($string) returns string, boolean {...}
As the author of the function, I will know if I have a logic error when this function I've
written doesn't return the pre-defined types and I get an error.
As a client programer (who will later use the above function, but hasn't written it), I will
know at a glance what output I can expect from a function, without scanning through the body to
deduce it.
For backwards compatibility, flexibility and php-ishness, I don't think this should be
required, but optional.
function my_string_function_1() {...}
function my_string_function_2() returns string, boolean {...}
function my_string_function_3() returns string, boolean, array {...}
Test script:
---------------
<?php
function my_string_function($string) returns array, string, boolean {
// do nothing; syntax presently invalid.
}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=68126&edit=1