# [RFC] Return type-hint

- Thread: https://externals.io/message/49158
- Original archive: https://news-web.php.net/php.internals/49158
- Started by Felipe Pena on 2010-07-29T01:49:03+00:00
- Messages: 48
- Page 1 of 2: messages 1 to 25 (25 per page, add `?page=N` to the URL)
- Next page: https://externals.io/message/49158.md?page=2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #49158 — Felipe Pena — 2010-07-29T01:49:03+00:00

URL: https://externals.io/message/49158#49158

Hi all,
I've updated the patch and the RFC that proposes the return type-hint
implementation (Engine + Reflection).
The proposed implementation is working just like the last changes in the
parameter type-hint (in trunk). i.e. working with the "scalar" and "numeric"
pseudo-types.

http://wiki.php.net/rfc/returntypehint

Thoughts?

-- 
Regards,
Felipe Pena

---

## #49159 — Christian Kaps — 2010-07-29T07:32:26+00:00

URL: https://externals.io/message/49158#49159 · In reply to: #49158

I would like to see this in the next major/minor release, together with
the traits and the type-hint implementation. This would be a great language
improvement.

From my end-developer standpoint a big +++



On Wed, 28 Jul 2010 22:49:03 -0300, Felipe Pena <felipensp@gmail.com>
wrote:
> Hi all,
> I've updated the patch and the RFC that proposes the return type-hint
> implementation (Engine + Reflection).
> The proposed implementation is working just like the last changes in the
> parameter type-hint (in trunk). i.e. working with the "scalar" and
> "numeric"
> pseudo-types.
> 
> http://wiki.php.net/rfc/returntypehint
> 
> Thoughts?

---

## #49160 — Ferenc Kovacs — 2010-07-29T07:43:20+00:00

URL: https://externals.io/message/49158#49160 · In reply to: #49158

> Hi all,
> I've updated the patch and the RFC that proposes the return type-hint
> implementation (Engine + Reflection).
> The proposed implementation is working just like the last changes in the
> parameter type-hint (in trunk). i.e. working with the "scalar" and "numeric"
> pseudo-types.
>
> http://wiki.php.net/rfc/returntypehint
>
> Thoughts?
>
> --
> Regards,
> Felipe Pena
>


Hi, I would love this feature.
But I have some concerns.
AFAIK you can declare classes with the name int ,scalar, etc. so
hinting anything else than class names can be ambiguous.
Or we have to set these keywords as reserved, so nobody could declare
classes with such names.
But in this case, this would be a big BC problem, especially, because
I think that there is many project out there which uses type
names(int, object, etc.) as classname.

Try to search "class Object{" or "class Object extends" on google codesearch.

Maybe this was resolved with the last scalar type hinting patch, which
got merged to the trunk, I don't know.

ps: the spam filter rejected my previous email because of the
codesearch links...

Tyrael

---

## #49161 — Daniel Egeberg — 2010-07-29T07:47:38+00:00

URL: https://externals.io/message/49158#49161 · In reply to: #49160

> 
> > […]
>
>
> Hi, I would love this feature.
> But I have some concerns.
> AFAIK you can declare classes with the name int ,scalar, etc. so
> hinting anything else than class names can be ambiguous.
> Or we have to set these keywords as reserved, so nobody could declare
> classes with such names.

Weren't they already made reserved with the parameter type hinting patch?

> But in this case, this would be a big BC problem, especially, because
> I think that there is many project out there which uses type
> names(int, object, etc.) as classname.
>
> Try to search "class Object{" or "class Object extends" on google codesearch.
>
> Maybe this was resolved with the last scalar type hinting patch, which
> got merged to the trunk, I don't know.
>
> ps: the spam filter rejected my previous email because of the
> codesearch links...
>
> Tyrael
>
> --
> 
> 
>
>



-- 
Daniel Egeberg

---

## #49163 — Ferenc Kovacs — 2010-07-29T07:58:07+00:00

URL: https://externals.io/message/49158#49163 · In reply to: #49161

> > […]
>
> Weren't they already made reserved with the parameter type hinting patch?
>

As I said:

> Maybe this was resolved with the last scalar type hinting patch, which
> got merged to the trunk, I don't know.

Tyrael

---

## #49169 — Felipe Pena — 2010-07-29T11:57:12+00:00

URL: https://externals.io/message/49158#49169 · In reply to: #49163

Hi

2010/7/29 Ferenc Kovacs <info@tyrael.hu>

> > […]
>
> As I said:
>
> > […]
>

Currently on trunk version, if you use the php native types as type-hint it
will check for the php type, not the user class.

Trunk:
<?php

class int { }

function test(int $a) { }

test(1); // ok
test(new int); // error

Which is just like the return type-hint is working. I do prefer offer a way
to continue using the user class 'Int', as we not going to add new
keywords... My suggestion (I guess already told it in some mail...) is to
identify the native php type just when it's lowercased (case-sensitive).
(Java-style)

Patch: http://felipe.ath.cx/diff/param-typehint-native-type.diff

Example:
<?php

class int { }

function test(Int $a) { } // user class 'Int'
function test2(int $a) { } // native type 'int'

test(new int); // ok
test2(1); // ok



Thanks for the feedbacks. :-)

-- 
Regards,
Felipe Pena

---

## #49170 — Josh Davis — 2010-07-29T12:17:29+00:00

URL: https://externals.io/message/49158#49170 · In reply to: #49169

> My suggestion (I guess already told it in some mail...) is to
> identify the native php type just when it's lowercased (case-sensitive).

Alternatively, one could use the full qualified name to refer to the
class name, e.g.

function expectsScalar(string $str) {}
function expectsObject(\string $obj) {}

-JD

---

## #49183 — Victor Bolshov — 2010-07-30T04:46:33+00:00

URL: https://externals.io/message/49158#49183 · In reply to: #49170

+1 for "one could use the full qualified name to refer to the class name".

Making the developer care about the case of characters in one special case -
that's the sort of changes that lead to chaos. Remember that type conversion
works in a case-insensitive manner and so does most of the language
constructs:

(int) $x === (INT) $x
intvav($x) === INTVAL($x)
and so on

2010/7/29 Josh Davis <phpwnd@gmail.com>

> 
> > […]
>
> Alternatively, one could use the full qualified name to refer to the
> class name, e.g.
>
> function expectsScalar(string $str) {}
> function expectsObject(\string $obj) {}
>
> -JD
>
> --
> 
> 
>
>


-- 
С уважением,
Виктор

---

## #52021 — Ferenc Kovacs — 2011-04-27T07:23:49+00:00

URL: https://externals.io/message/49158#52021 · In reply to: #49158

> Hi all,
> I've updated the patch and the RFC that proposes the return type-hint
> implementation (Engine + Reflection).
> The proposed implementation is working just like the last changes in the
> parameter type-hint (in trunk). i.e. working with the "scalar" and
> "numeric"
> pseudo-types.
>
> http://wiki.php.net/rfc/returntypehint
>
> Thoughts?
>
> --
> Regards,
> Felipe Pena
>

for the upcoming 5.4 release, I think it would be extremely useful, even
without the scalar stuff.

Tyrael

---

## #52023 — pasthelod@gmail.com — 2011-04-27T09:29:35+00:00

URL: https://externals.io/message/49158#52023 · In reply to: #52021

> 
>
> > […]
>
> for the upcoming 5.4 release, I think it would be extremely useful, even
> without the scalar stuff.
>
> Tyrael
>


Hi,

Now that someone else necromanced this thread back to life, may I ask
which variant of Type Hinting have emerged from the debate as the
candidate for implementation?

Also, I seem to remember a variant with both strict and weak type
hinting/checking, using parentheses to distinguish behavior. What
happened to that, never made it to an RFC?

-- 
Pas

---

## #52024 — Ferenc Kovacs — 2011-04-27T09:52:24+00:00

URL: https://externals.io/message/49158#52024 · In reply to: #52023

> 
> > […]
> wrote:
> > […]
>
>
> Hi,
>
> Now that someone else necromanced this thread back to life, may I ask
> which variant of Type Hinting have emerged from the debate as the
> candidate for implementation?
>
> Also, I seem to remember a variant with both strict and weak type
> hinting/checking, using parentheses to distinguish behavior. What
> happened to that, never made it to an RFC?
>
> --
> Pas
>
> --
> 
> 
>
>




> 
> > […]
> wrote:
> > […]
>
>
> Hi,
>
> Now that someone else necromanced this thread back to life, may I ask
> which variant of Type Hinting have emerged from the debate as the
> candidate for implementation?
>
> Also, I seem to remember a variant with both strict and weak type
> hinting/checking, using parentheses to distinguish behavior. What
> happened to that, never made it to an RFC?
>
> --
> Pas
>
> --
> 
> 
>
>
https://wiki.php.net/rfc/typechecking
Ilia's proposal was implemented, then reverted, and Derick's proposal was
commited, but judging from the mails on internals, this will/could
bereverted from the next release.
the other proposals was never implemented.

ps: we have a(many) different thread for scalar type-hint discussion

Tyrael

---

## #52026 — Arvids Godjuks — 2011-04-27T10:02:27+00:00

URL: https://externals.io/message/49158#52026 · In reply to: #52023

There was no consensus on that, but as i remember the tendency was more to
the weak type hinting because of dynamic nature of PHP. You know - all the
http data comes as strings, most db data comes as strings and so on.
27.04.2011 11:30 пользователь "Pas" <pasthelod@gmail.com> написал:
> 
> > […]
>
>
> Hi,
>
> Now that someone else necromanced this thread back to life, may I ask
> which variant of Type Hinting have emerged from the debate as the
> candidate for implementation?
>
> Also, I seem to remember a variant with both strict and weak type
> hinting/checking, using parentheses to distinguish behavior. What
> happened to that, never made it to an RFC?
>
> --
> Pas

---

## #52044 — Stas Malyshev — 2011-04-28T08:23:02+00:00

URL: https://externals.io/message/49158#52044 · In reply to: #52021

Hi!

> > […]
> for the upcoming 5.4 release, I think it would be extremely useful, even
> without the scalar stuff.


Personally, I see even less point in having strict return typing (please 
stop using "hint" terminology, it confuses the whole matter, it's not 
"hint" if it describes mandatory restriction on the data) than strict 
argument typing. The RFC doesn't explain it either beyond "here how you 
can have some nice errors".
But why would I want to see these errors? How they would make anything 
easier or better?
-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #52045 — Reindl Harald — 2011-04-28T08:31:16+00:00

URL: https://externals.io/message/49158#52045 · In reply to: #52044

Am 28.04.2011 10:23, schrieb Stas Malyshev:

> But why would I want to see these errors? 
> How they would make anything easier or better?

to find problems before your users?

i do not know what you want, but i want with
"error_reporting = `E_ALL` | E_STRICT" get noticed if
something possible is unclean

it is the same as for functions - so what is the difference
between the following? the first one can get a empty string
and will not notice you and ignore the foreach

the second one will produce an error if empty string is given
while an empty array would be accepted

function bla($arr)
{
 if(!empty($arr))
 {
  foreach($arr)
  {
  }
 }
}

function bla(array $arr)
{
 if(!empty($arr))
 {
  foreach($arr)
  {
  }
 }
}

---

## #52046 — Stas Malyshev — 2011-04-28T08:35:00+00:00

URL: https://externals.io/message/49158#52046 · In reply to: #52045

Hi!

> to find problems before your users?

Sorry, this doesn't say much. It's a generic, of course we agree it's 
good to find bugs sooner rather than later. The question is why would 
that specific thing that is being proposed help - in which specific way?

> the second one will produce an error if empty string is given
> while an empty array would be accepted

And it is better because?..
Also please note we are discussing strict return typing.
-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #52047 — Ferenc Kovacs — 2011-04-28T08:39:55+00:00

URL: https://externals.io/message/49158#52047 · In reply to: #52044

On Thu, Apr 28, 2011 at 10:23 AM, Stas Malyshev <smalyshev@sugarcrm.com>wrote:

> Hi!
>
>  http://wiki.php.net/rfc/returntypehint
> > […]
>
>
> Personally, I see even less point in having strict return typing (please
> stop using "hint" terminology, it confuses the whole matter, it's not "hint"
> if it describes mandatory restriction on the data) than strict argument
> typing. The RFC doesn't explain it either beyond "here how you can have some
> nice errors".
> But why would I want to see these errors? How they would make anything
> easier or better?
> --
> Stanislav Malyshev, Software Architect
> SugarCRM: http://www.sugarcrm.com/
> (408)454-6900 ext. 227
>

do you aware that without the scalar support, the return type hinting would
behave just as "strict" as the argument type hinting is currently.
http://php.net/manual/en/language.oop5.typehinting.php

so maybe type hinting wasn't a good name at the first place, but we should
be consistent with the current naming convention.

Tyrael

---

## #52049 — Ferenc Kovacs — 2011-04-28T08:52:34+00:00

URL: https://externals.io/message/49158#52049 · In reply to: #52044

>
>
>
> The RFC doesn't explain it either beyond "here how you can have some nice
> errors".
> But why would I want to see these errors? How they would make anything
> easier or better?
> --
> Stanislav Malyshev, Software Architect
> SugarCRM: http://www.sugarcrm.com/
> (408)454-6900 ext. 227
>

maybe we should improve on the rfc.
I see many advantages of the introduction of return type hinting.
- you can guarantee that your methods returns only what is explicitly stated
(or else it blows up, the same as for arguments)
- it can save you from having to define the return type in phpdoc, your code
can define it's behavior
- I think maybe the language itself could use that information, for
performance, etc. improvements (we know that the return value will be
created runtime, but we can throw it away if it doesn't match the return
type or something), maybe we can use that information to throw errors on
compile time if we find a non runtime decided return value which doesn't
match with the return type.

I'm sure that others have and will come up with more use cases.

Tyrael

---

## #52050 — Alain Williams — 2011-04-28T08:58:59+00:00

URL: https://externals.io/message/49158#52050 · In reply to: #52046

> Hi!
> 
> > […]
> 
> Sorry, this doesn't say much. It's a generic, of course we agree it's 
> good to find bugs sooner rather than later. The question is why would 
> that specific thing that is being proposed help - in which specific way?

It would mean that the code that follows could benefit from the PHP run
time checking the return type -- at the point of return. Thus eliminating
following errors where the user does not check, eg:

    function array foo() { .... }

    $ret = foo();
    foreach($ret as $val)
        ....

I must admit that this is much less useful than checking of argument types
going into functions, where the writer of the function (part of a libarary perhaps)
does not trust the function user to get validation/... right.
But this is another discussion.


The other way in that this could be very helpful is with run time optimisation, especially
for things like HipHop - think of the optimisation that it could make if it knew
the real return type, eg:

    function int bar() { ... }

(The argument about argument types is even more so here.)

> > […]
> 
> And it is better because?..
> Also please note we are discussing strict return typing.


I am also mindful that the proposed syntax does not allow description
of functions that return more than one type; where they return something
to indicate a failure (or end) - typically `FALSE` or NULL. So maybe something like
the following could cope with them:

    function (array | bool) baz() { ... }

or even:

    function (array | false) baz() { ... }

I can see that some may prefer commas:

    function (array, false) baz() { ... }

although, to me, that looks like a list - I prefer vertical bar.

HipHop would be able to optimise things like:

    if( ($ret = baz()) !== `FALSE` ) {
        ...
    }


[**] https://github.com/facebook/hiphop-php/wiki/

-- 
Alain Williams
Linux/GNU Consultant - Mail systems, Web sites, Networking, Programmer, IT Lecturer.
+44 (0) 787 668 0256  http://www.phcomp.co.uk/
Parliament Hill Computers Ltd. Registration Information: http://www.phcomp.co.uk/contact.php
#include <std_disclaimer.h

---

## #52051 — Alain Williams — 2011-04-28T09:02:59+00:00

URL: https://externals.io/message/49158#52051 · In reply to: #52047

> do you aware that without the scalar support, the return type hinting would
> behave just as "strict" as the argument type hinting is currently.
> http://php.net/manual/en/language.oop5.typehinting.php
> 
> so maybe type hinting wasn't a good name at the first place, but we should
> be consistent with the current naming convention.

Yes, it was a poor choice of words. I think that it should be renamed 'type checking'
with a transition note, eg:

	Type checking (formerly called type hinting)

-- 
Alain Williams
Linux/GNU Consultant - Mail systems, Web sites, Networking, Programmer, IT Lecturer.
+44 (0) 787 668 0256  http://www.phcomp.co.uk/
Parliament Hill Computers Ltd. Registration Information: http://www.phcomp.co.uk/contact.php
#include <std_disclaimer.h

---

## #52052 — Rasmus Lerdorf — 2011-04-28T09:43:36+00:00

URL: https://externals.io/message/49158#52052 · In reply to: #52050

> 
> > […]
>
> It would mean that the code that follows could benefit from the PHP run
> time checking the return type -- at the point of return. Thus eliminating
> following errors where the user does not check, eg:
>
>      function array foo() { .... }
>
>      $ret = foo();
>      foreach($ret as $val)
>          ....

I don't see this being useful at all. All you are doing is duplicating a 
runtime warning. We can't possibly know at compile-time whether this 
return value is an array or not, so there is no compile-time benefit.

eg.

function array foo() {
   $ret = bar();
   return $ret;
}

So in this case we get a warning on the return and then another warning 
on the foreach(). We have not gained anything except an extra warning 
for the same problem in this code example.

And this gets even worse if you start doing scalar return type checking. 
Now you are adding warnings for types that are safely interchangeable.

-Rasmus

---

## #52053 — Alain Williams — 2011-04-28T09:59:58+00:00

URL: https://externals.io/message/49158#52053 · In reply to: #52052

> > […]
> 
> I don't see this being useful at all. All you are doing is duplicating a 
> runtime warning. We can't possibly know at compile-time whether this 
> return value is an array or not, so there is no compile-time benefit.

The check is not done where foo() is called but within foo() itself, to ensure
that it only returns an array.

There will also be advantages for HipHop which can afford to spend the time to
do static analysis of code -- I know that HipHop is not your baby
but you now need to recognise that there is more than one PHP implementation
and features that may not had much advantage with Zend may be useful elsewhere.

-- 
Alain Williams
Linux/GNU Consultant - Mail systems, Web sites, Networking, Programmer, IT Lecturer.
+44 (0) 787 668 0256  http://www.phcomp.co.uk/
Parliament Hill Computers Ltd. Registration Information: http://www.phcomp.co.uk/contact.php
#include <std_disclaimer.h

---

## #52056 — Martin Scotta — 2011-04-28T13:30:43+00:00

URL: https://externals.io/message/49158#52056 · In reply to: #52053

Hey All,
I'm really interested on this topic.

What would be the result of this?

function Foo foo($something) {
     if ( $something ) {
          return new Foo;
     }
     // no return implies return null
}

How do you force to return a Type with null allowed?

 Martin Scotta




> 
>
> > […]
>
> The check is not done where foo() is called but within foo() itself, to
> ensure
> that it only returns an array.
>
> There will also be advantages for HipHop which can afford to spend the time
> to
> do static analysis of code -- I know that HipHop is not your baby
> but you now need to recognise that there is more than one PHP
> implementation
> and features that may not had much advantage with Zend may be useful
> elsewhere.
>
> --
> Alain Williams
> Linux/GNU Consultant - Mail systems, Web sites, Networking, Programmer, IT
> Lecturer.
> +44 (0) 787 668 0256  http://www.phcomp.co.uk/
> Parliament Hill Computers Ltd. Registration Information:
> http://www.phcomp.co.uk/contact.php
> #include <std_disclaimer.h

---

## #52057 — Alain Williams — 2011-04-28T13:39:15+00:00

URL: https://externals.io/message/49158#52057 · In reply to: #52056

> Hey All,
> I'm really interested on this topic.
> 
> What would be the result of this?
> 
> function Foo foo($something) {
>      if ( $something ) {
>           return new Foo;
>      }
>      // no return implies return null
> }
> 
> How do you force to return a Type with null allowed?

That is why I suggested, in addition to the above, a syntax like:

    function (Foo | NULL) foo($something) {

to be able to specify where a function can return more than one type.
The 'extra' types/values are nearly always either `NULL` or FALSE.

Regards

-- 
Alain Williams
Linux/GNU Consultant - Mail systems, Web sites, Networking, Programmer, IT Lecturer.
+44 (0) 787 668 0256  http://www.phcomp.co.uk/
Parliament Hill Computers Ltd. Registration Information: http://www.phcomp.co.uk/contact.php
#include <std_disclaimer.h

---

## #52058 — Ben Schmidt — 2011-04-28T15:58:02+00:00

URL: https://externals.io/message/49158#52058 · In reply to: #52044

FWIW, I don't see much point in this either, as far as error reporting is 
concerned. I think type checking for parameters can provide cheap and useful 
validation, as parameters passed in are outside the function's control. But return 
values? They are within the function's control, and in most functions are not hard 
to ensure are correct without needing runtime checking.

If PHP were compiled, not interpreted, i.e. less dynamic, there could be some 
benefits, as compile-time optimisations could perhaps be built in, and validations 
skipped if they were provably unnecessary. But that's not going to happen any time 
soon, as far as I'm aware.

So the only real benefit is that they could make code slightly self-documenting. 
But since they would still be optional, they don't help much: the coder might as 
well take up the option to write some more useful documentation.

Also, PHP has so many conventions such as returning false on error that don't fit 
cleanly into the model, that it's awkward. I'd love to see those problems solved 
neatly for function arguments before extending anything to function return types.

I don't think it would do any harm though, so don't see any argument against it. I 
just don't think there's a good argument for it.

Ben.




> Hi!
>
> > […]
>
>
> Personally, I see even less point in having strict return typing (please stop
> using "hint" terminology, it confuses the whole matter, it's not "hint" if it
> describes mandatory restriction on the data) than strict argument typing. The RFC
> doesn't explain it either beyond "here how you can have some nice errors".
> But why would I want to see these errors? How they would make anything easier or
> better?

---

## #52059 — Alain Williams — 2011-04-28T16:10:43+00:00

URL: https://externals.io/message/49158#52059 · In reply to: #52058

> If PHP were compiled, not interpreted, i.e. less dynamic, there could be 
> some benefits, as compile-time optimisations could perhaps be built in, and 
> validations skipped if they were provably unnecessary. But that's not going 
> to happen any time soon, as far as I'm aware.

It has already happened:

	https://github.com/facebook/hiphop-php/wiki/

It might also be useful for reflection.

-- 
Alain Williams
Linux/GNU Consultant - Mail systems, Web sites, Networking, Programmer, IT Lecturer.
+44 (0) 787 668 0256  http://www.phcomp.co.uk/
Parliament Hill Computers Ltd. Registration Information: http://www.phcomp.co.uk/contact.php
#include <std_disclaimer.h

---

Next page: https://externals.io/message/49158.md?page=2
