Re: [RFC] Return Type Declarations

From: Date: Wed, 07 May 2014 22:30:00 +0000
Subject: Re: [RFC] Return Type Declarations
References: 1 2 3 4 5 6 7  Groups: php.internals 
Request: Send a blank email to internals+get-74039@lists.php.net to get a copy of this message
On Wed, May 7, 2014 at 11:02 AM, Lin Yo-An <cornelius.howl@gmail.com> wrote:

> Hi,
>
> Why using an additional ":" symbol for the return type? what about
> considering this:
>
>   function foo() string {  }
>
> and for closure:
>
>   $bar = 1;
>   $foo = function() use ($bar) string {
>
>   };
>
> I think removing ":" symbol is cleaner.


Your point is valid, certainly. We could omit it. Here are two minor things
to think about:

 1. Syntax is a visual indicator.
 2. Compatibility and familiarity to similar languages.

Syntax can be used to visually indicate things to the programmer. For
instance, the () in a conditional statement is not strictly necessary
from a language design perceptive and Go chose to omit them. Valid Go:

if $var === $null {

}

In our case the colon indicates that you are declaring a return type and
you can read it aloud as "returns". While not strictly necessary it does
give a visual indicator of what is going on.

function date_diff(DateTime $a, DateTime $b) : DateInterval;

The above function can be read aloud as:

"Function date_diff takes DateTime a and DateTime b and returns
DateInterval"

The colon indicates 'returns'. You can infer this from the context, sure;
again I did say this was a minor thing.

In the case of the colon I value being compatible with Hack more than the
small gain of omitting the colon.


Thread (28 messages)

« previous php.internals (#74039) next »