Re: Types on the right or on the left
| From: | Andrea Faulds | Date: | Tue, 04 Nov 2014 16:28:56 +0000 |
| Subject: | Re: Types on the right or on the left | ||
| References: | 1 2 3 4 5 6 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-78647@lists.php.net to get a copy of this message | ||
> On 4 Nov 2014, at 11:50, Chris Wright <cw@daverandom.com> wrote:
>
> On 4 November 2014 10:32, Andrea Faulds <ajf@ajf.me> wrote:
>
> > I thought it was inconsistent, but after discussions on StackOverflow, I don't think
> > it actually is. > Return types describe the return type of a function, not the type of a
> > function. So there's no reason > they have to go before the function name like parameter
> > types do.
>
> So, in a sentence:
>
> In PHP, input types go on the left and output types go on the right.
>
> This sounds like a convention that could be established and followed, and no-one could
> reasonably call it inconsistent if it was adhered to.
>
That’s not quite what I meant, but I’d also agree with that convention.
For parameters, the type is of the thing itself:
function foo(Foo $foo, Bar $bar);
Here, $foo is of the type Foo, and $bar is of the type Bar.
But say if we were to add return types like this:
function FooBar qux();
The type of qux isn’t a FooBar, it’s a function (more accurately, an argument-less,
return-anything function). FooBar isn’t what qux is, it’s not qux’s type, merely what it
returns. In a sense, I’d say there’s already a type here:
function.
Compare it to this:
function qux(): FooBar;
FooBar is what qux returns, function is what qux is.
If we were to hypothetically add typed properties with the same syntax Hack has, they’d look like
this:
public Foo $foo;
This would be consistent with parameters. And I think it goes along well with return types at the
end:
public Foo $foo;
public function foobar(): FooBar;
Here, it’s clear the second item is a function, and the previous item is a property of type Foo.
…but hey, I could be wrong. Does this make sense to anyone?
--
Andrea Faulds
http://ajf.me/