# [RFC] Void Return Type (v0.2, reöpening)

- Thread: https://externals.io/message/88792
- Original archive: https://news-web.php.net/php.internals/88792
- Started by Andrea Faulds on 2015-10-14T21:52:34+00:00
- Messages: 43
- RFC: Void Return Type (Implemented (PHP 7.1)) — https://externals.io/rfc/void_return_type.md
- Page 1 of 2: messages 1 to 25 (25 per page, add `?page=N` to the URL)
- Next page: https://externals.io/message/88792.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

---

## #88792 — Andrea Faulds — 2015-10-14T21:52:34+00:00

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

Good evening,

I'm reviving my Void Return Type RFC, this time for PHP 7.1:

https://wiki.php.net/rfc/void_return_type

Please read it and tell me your thoughts!

Thanks.

P.S. As it so (fatefully?) happens, I originally introduced this on 14th 
February, and it's now 14th October, so it's been exactly 8 months!

-- 
Andrea Faulds
http://ajf.me/

---

## #88793 — Stanislav Malyshev — 2015-10-14T22:00:30+00:00

URL: https://externals.io/message/88792#88793 · In reply to: #88792

Hi!

> I'm reviving my Void Return Type RFC, this time for PHP 7.1:
> 
> https://wiki.php.net/rfc/void_return_type
> 
> Please read it and tell me your thoughts!

I still see no point in this, as every PHP function actually returns
something (at least null). So this type would not actually be right and
would not reflect what actually is happening.

Moreover, I don't see any scenario where some code depends on a function
not returning something explicitly (i.e. does not use return value) but
function actually returns something explicitly and the calling code
breaks. This appears to be impossible, and thus there's no reason to
declare a function "void" except purely for documentation purposes.
Adding keywords to the language just to document this does not look to
me like a good idea.

-- 
Stas Malyshev
smalyshev@gmail.com

---

## #88794 — Andrea Faulds — 2015-10-14T22:23:15+00:00

URL: https://externals.io/message/88792#88794 · In reply to: #88793

Hi Stas,

Stanislav Malyshev wrote:
>
> I still see no point in this, as every PHP function actually returns
> something (at least null). So this type would not actually be right and
> would not reflect what actually is happening.

It wouldn't be incorrect. Not all languages with `void` prevent a 
function's use as an expression (or 'rvalue' in C parlance). In some, it 
merely requires the function not to explicitly return a value.

And, heck, we use void in the PHP manual all the time.

> Moreover, I don't see any scenario where some code depends on a function
> not returning something explicitly (i.e. does not use return value) but
> function actually returns something explicitly and the calling code
> breaks. This appears to be impossible,

It most likely is, but it's not the only case that matters. What if you 
mistakenly return a value in a function that's not supposed to? This 
would catch you out.

> and thus there's no reason to
> declare a function "void" except purely for documentation purposes.
> Adding keywords to the language just to document this does not look to
> me like a good idea.

I feel that the basic facts about a function should be in the signature, 
and shouldn't have to be hidden in a comment, if only because it's more 
concicse and keeps information about parameters on the same line as the 
parameters themselves. Unlike comments, type hints can't lie, because 
they're actually enforced.

But, yes, it is mostly just useful for documentation. I can understand 
your objection there.

Thanks.
-- 
Andrea Faulds
http://ajf.me/

---

## #88795 — Stanislav Malyshev — 2015-10-14T22:43:08+00:00

URL: https://externals.io/message/88792#88795 · In reply to: #88794

Hi!

> And, heck, we use void in the PHP manual all the time.

Yes, because PHP manual *is* the documentation :) Note that function
descriptions in the manual, while they are similar to PHP syntax, aren't
actually valid PHP, even after scalar typing introduction. Bizarrely
enough, they don't even match the return type syntax.

> It most likely is, but it's not the only case that matters. What if you
> mistakenly return a value in a function that's not supposed to? This
> would catch you out.

Why would I care about such a mistake? Why would I care so much that I
would need a special language-level construct to catch it? How many such
mistakes happen and are they critical enough to have the engine
bothering with going after them? I think such mistakes are extremely
rare and insignificant.

> I feel that the basic facts about a function should be in the signature,
> and shouldn't have to be hidden in a comment, if only because it's more

I guess we disagree here, because I do not think comments are "hidden",
and I do not think signature of the function should be the comment. In
fact, in Java for example (which seems to be the model language for the
strict typing aficionados) it is long standing best practice to actually
verify the javadoc comments as part of the build and fail the build if
they are missing or wrong. I am not advocating for doing (or not doing
:) this in PHP, but I do say documentation is not "hidden" and strict
typing is not the same as documentation. Moreover, in this case strict
typing implies something that is outright false - the function that is
declared void does return a value, namely null. Even worse, if you do
try to return exactly the same value, the RFC requires failure in this
case, which makes zero sense as precisely the same thing happens in both
cases.

> concicse and keeps information about parameters on the same line as the
> parameters themselves. Unlike comments, type hints can't lie, because
> they're actually enforced.

In fact, it is the opposite - as I said above, that particular type does
lie, and it has to, because what it tries to express actually does not
exist in PHP.
I might be reluctantly ok with tolerating purely documentation type, but
tolerating purely documentation type that is in fact untrue does not
look good to me.
-- 
Stas Malyshev
smalyshev@gmail.com

---

## #88796 — Levi Morrison — 2015-10-14T22:46:17+00:00

URL: https://externals.io/message/88792#88796 · In reply to: #88793

> Hi!
>
> > […]
>
> I still see no point in this, as every PHP function actually returns
> something (at least null). So this type would not actually be right and
> would not reflect what actually is happening.

I agree that `void` doesn't make sense given that we document that
`return;` will return null[1].  If the union types RFC[2] passes it
makes sense to allow `Foo | null` which allows something of type `Foo`
or `null`. To me it makes sense that if you then remove `Foo` you are
left with `null`, not `void`. My personal recommendation because of
this would be to use `null` for the return type and instead of `void`.

Also, I do not think this feature is pointless. For instance, it has
value in interfaces. Declaring that a method does not return a value
means implementors cannot accidentally return something. This is
better than just documenting that it should not be done.

  [1]: http://php.net/manual/en/function.return.php
  [2]: https://wiki.php.net/rfc/union_types

---

## #88797 — Ryan Pallas — 2015-10-14T22:50:41+00:00

URL: https://externals.io/message/88792#88797 · In reply to: #88796

>
>
> I agree that `void` doesn't make sense given that we document that
> `return;` will return null[1].  If the union types RFC[2] passes it
> makes sense to allow `Foo | null` which allows something of type `Foo`
> or `null`. To me it makes sense that if you then remove `Foo` you are
> left with `null`, not `void`. My personal recommendation because of
> this would be to use `null` for the return type and instead of `void`.
>
> Also, I do not think this feature is pointless. For instance, it has
> value in interfaces. Declaring that a method does not return a value
> means implementors cannot accidentally return something. This is
> better than just documenting that it should not be done.


+1 these are my thoughts exactly. I would be for it if it was null, and
interfaces is exactly why I would be for it.

---

## #88798 — Korvin Szanto — 2015-10-14T22:55:07+00:00

URL: https://externals.io/message/88792#88798 · In reply to: #88796

> On Wed, Oct 14, 2015 at 4:00 PM, Stanislav Malyshev <smalyshev@gmail.com>
> wrote:
> > […]
>
> I agree that `void` doesn't make sense given that we document that
> `return;` will return null[1].  If the union types RFC[2] passes it
> makes sense to allow `Foo | null` which allows something of type `Foo`
> or `null`. To me it makes sense that if you then remove `Foo` you are
> left with `null`, not `void`. My personal recommendation because of
> this would be to use `null` for the return type and instead of `void`.
>

Isn't null supposed to mean the absense of a value and not take the place
of a value in PHP though? Didn't we just have a long conversation about
this in a thread about PHPSadness?

If I capture the result of a "void" method and check if my result variable
with isset(), I'll get false. This sounds like it's void of value to me.


> Also, I do not think this feature is pointless. For instance, it has
> value in interfaces. Declaring that a method does not return a value
> means implementors cannot accidentally return something. This is
> better than just documenting that it should not be done.
>

This is what I was thinking too, I'm not sure that there's much value
beyond that


>   [1]: http://php.net/manual/en/function.return.php
>   [2]: https://wiki.php.net/rfc/union_types

---

## #88799 — Andrea Faulds — 2015-10-14T23:00:31+00:00

URL: https://externals.io/message/88792#88799 · In reply to: #88796

Hi Levi,

Levi Morrison wrote:
> 
> > […]
>
> I agree that `void` doesn't make sense given that we document that
> `return;` will return null[1].

Both you and Stas have said this, but it's only true if we solely 
consider C-like languages. Other languages do different things. In the 
PHP manual, Hack, TypeScript, ActionScript, and most likely other 
languages (these are just off the top of my head), `void` functions do 
still have an implicit result.

All of these languages would have had the choice to do what you're 
suggesting and use `null`, or its equivalent (`undefined` for TypeScript 
and ActionScript). They didn't. Why? If I had to guess, there's at least 
three reasons. For one, void is the word languages usually use for this. 
For another, `void` and `null` they mean different things. `void` 
signifies a function isn't returning anything. `null` signifies a 
function that *returns null*, regardless of where that null came from. 
`function foo(): null { return some_probably_null_returning_function(); 
}` should surely be legal with a `null` type hint, yet it's nonsensical 
code. Finally, making a function truly "return nothing", i.e. 
disallowing its use as an expression/rvalue, breaks some use cases, like 
passing along the result of a callback.

PHP would neither be the first nor the last to be using `void` in this way.

> If the union types RFC[2] passes it
> makes sense to allow `Foo | null` which allows something of type `Foo`
> or `null`. To me it makes sense that if you then remove `Foo` you are
> left with `null`, not `void`. My personal recommendation because of
> this would be to use `null` for the return type and instead of `void`.

`null` would be a weird type, because it doesn't make sense as a 
parameter type, and as a return type, you don't really want to enforce 
returning null, you want to enforce not returning at all (see the 
example above). It feels like a poor man's substitute to me.

Thanks.

-- 
Andrea Faulds
http://ajf.me/

---

## #88800 — Larry Garfield — 2015-10-14T23:19:13+00:00

URL: https://externals.io/message/88792#88800 · In reply to: #88799

> Both you and Stas have said this, but it's only true if we solely 
> consider C-like languages. Other languages do different things. In the 
> PHP manual, Hack, TypeScript, ActionScript, and most likely other 
> languages (these are just off the top of my head), `void` functions do 
> still have an implicit result.
>
> All of these languages would have had the choice to do what you're 
> suggesting and use `null`, or its equivalent (`undefined` for 
> TypeScript and ActionScript). They didn't. Why? If I had to guess, 
> there's at least three reasons. For one, void is the word languages 
> usually use for this. For another, `void` and `null` they mean 
> different things. `void` signifies a function isn't returning 
> anything. `null` signifies a function that *returns null*, regardless 
> of where that null came from. `function foo(): null { return 
> some_probably_null_returning_function(); }` should surely be legal 
> with a `null` type hint, yet it's nonsensical code. Finally, making a 
> function truly "return nothing", i.e. disallowing its use as an 
> expression/rvalue, breaks some use cases, like passing along the 
> result of a callback.
>
> PHP would neither be the first nor the last to be using `void` in this 
> way.
>
> > […]
>
> `null` would be a weird type, because it doesn't make sense as a 
> parameter type, and as a return type, you don't really want to enforce 
> returning null, you want to enforce not returning at all (see the 
> example above). It feels like a poor man's substitute to me.
>
> Thanks.

The tricky part here is that saying a function does not return is not 
something PHP currently does:

https://3v4l.org/HtAuC

No return implicitly returns NULL, which you can assign to a variable 
if, for some strange reason, you were so inclined.  So this would be 
more than "just" a syntactic documentation feature.

Which I believe gives the following options:

1) Change the language behavior such that

function foo() : void { ...}
$a = foo();

Is a syntax error (because there really was nothing returned to assign), 
rather than resulting in $a having a value of NULL.

2) Use null as a "type" (which I agree feels weird just saying it), such 
that:

function foo() : null { ...}
$a = foo();

and

function foo() { ...}
$a = foo();

are identical.  The former would impact the contents of the function 
(eg, a non-empty return would be a parse error), but the external result 
is the same ($a == NULL).

3) Use the "void" keyword, but give it the same effect as option 2.

The RFC currently seems to propose option 3 (based on the "Use of void 
functions in expressions" section).  I don't have a strong feeling at 
this point about which option I'd prefer.

--Larry Garfield

---

## #88802 — Andrea Faulds — 2015-10-14T23:30:34+00:00

URL: https://externals.io/message/88792#88802 · In reply to: #88800

Hi Larry,

Larry Garfield wrote:
> The tricky part here is that saying a function does not return is not
> something PHP currently does:
>
> https://3v4l.org/HtAuC
>
> No return implicitly returns NULL, which you can assign to a variable
> if, for some strange reason, you were so inclined.  So this would be
> more than "just" a syntactic documentation feature.

Which has been pointed out.

> Which I believe gives the following options:
>
> 1) Change the language behavior such that
>
> function foo() : void { ...}
> $a = foo();
>
> Is a syntax error (because there really was nothing returned to assign),
> rather than resulting in $a having a value of NULL.

As the RFC notes, this breaks things, and is inconsistent with how PHP 
already does things.

> 2) Use null as a "type" (which I agree feels weird just saying it), such
> that:
>
> function foo() : null { ...}
> $a = foo();
>
> and
>
> function foo() { ...}
> $a = foo();
>
> are identical.  The former would impact the contents of the function
> (eg, a non-empty return would be a parse error), but the external result
> is the same ($a == NULL).

This would be strange. The manual doesn't say `null`, and I can't think 
of any language which uses `null` as the return type in this situation, 
even when they have the same implicit-null-return behaviour that PHP has 
(see the email you're replying to).

Also, wouldn't you expect this to behave like existing type hints, and 
let you return a null value from any source? But that's not what you 
would actually want, right?

Thanks.

-- 
Andrea Faulds
http://ajf.me/

---

## #88803 — Larry Garfield — 2015-10-15T01:16:35+00:00

URL: https://externals.io/message/88792#88803 · In reply to: #88802

> Which I believe gives the following options:
> > […]
>
> As the RFC notes, this breaks things, and is inconsistent with how PHP 
> already does things.
>
> > […]
>
> This would be strange. The manual doesn't say `null`, and I can't 
> think of any language which uses `null` as the return type in this 
> situation, even when they have the same implicit-null-return behaviour 
> that PHP has (see the email you're replying to).
>
> Also, wouldn't you expect this to behave like existing type hints, and 
> let you return a null value from any source? But that's not what you 
> would actually want, right?
>
> Thanks.

Well, that depends on the intent here.  In practice, is the intent to 
add a "no return at all" type of function (ie, $a = foo() is a syntax 
error now where it was not before), or to make it explicit that the only 
legal return is null (even if implicitly that means having a non-early 
return statement is pointless)?  Those are the two options.  That may or 
may not dictate the keyword that gets used.

It definitely sounds like you're favoring the second (as that's what the 
RFC says).  Which may or make not make "void" an odd keyword to choose 
when what's actually happening is `NULL` getting returned.  Is `NULL` a 
void?  (There's a deep philosophical question...)

As I said, I don't have a strong opinion on the subject yet.  I'm just 
trying to distil the discussion down to as small a question as possible. :-)

Cheers.

--Larry Garfield

---

## #88809 — Rowan Collins — 2015-10-15T08:43:04+00:00

URL: https://externals.io/message/88792#88809 · In reply to: #88798

Korvin Szanto wrote on 14/10/2015 23:55:
> If I capture the result of a "void" method and check if my result variable
> with isset(), I'll get false. This sounds like it's void of value to me.

But why "invent" (as far as PHP is concerned) this new keyword of "void" 
to mean exactly the same thing "null" already means - absence of a 
definite value?

---

## #88810 — Chris Riley — 2015-10-15T09:15:14+00:00

URL: https://externals.io/message/88792#88810 · In reply to: #88792

> Good evening,
>
> I'm reviving my Void Return Type RFC, this time for PHP 7.1:
>
> https://wiki.php.net/rfc/void_return_type
>
> Please read it and tell me your thoughts!
>
> Thanks.
>
> P.S. As it so (fatefully?) happens, I originally introduced this on 14th
> February, and it's now 14th October, so it's been exactly 8 months!
>
> --
> Andrea Faulds
> http://ajf.me/
>
> --
> 
> 
>
>
Hi,

Semantically, I don't believe that there is sufficient difference between
"returns no value" and "returns a value which has been defined as having no
value" for us to care about it. The main difference you get between return
type of null and a return type of void seems to be some extra fatal errors,
for a developer there seems little value.

From a user confusion point of view; PHP currently considers return; and
return null; to be equivalent (in fact some code style checkers will
replace one with the other) If (and only if) a function has a return type
of void these two statements are no longer equivalent - one is a fatal
error one is not. For any other return type specification, return null; and
return; would behave the same. This in itself would be enough for me to be
against adding a return type of void.

(also could someone enable my wiki acc: carnage; sent an email ~a week ago
and haven't heard back yet :()

~C

---

## #88812 — Rowan Collins — 2015-10-15T10:16:55+00:00

URL: https://externals.io/message/88792#88812 · In reply to: #88792

Andrea Faulds wrote on 14/10/2015 22:52:
> Good evening,
>
> I'm reviving my Void Return Type RFC, this time for PHP 7.1:
>
> https://wiki.php.net/rfc/void_return_type
>
> Please read it and tell me your thoughts!

My feeling is that it is odd to have both of these be true at once:

- void functions can appear in expressions, where the return value is 
interpreted as null
- "return;" and "return null;" are no longer treated as equivalent

If the intent is to say "you can use this in an expression, but its 
value will always be null so there's no point", then "return null;" 
seems just as valid as "return;", and a typehint of "null" seems to make 
more sense.

I take the point about making intent clear, but can't think of any other 
interpretation of "this function always returns null" than "this 
function has no meaningful result".  In other words, I can't imagine 
ever wanting to use "null" to mean something different from "void" in 
this context.


I can see the point in denying the right to say "return 
some_function_expected_to_return_null();" But in a sense this is no 
different from declaring that a function returns int, and then writing 
"return some_function_expected_to_return_int();"

If a void function can be used in an expression, it can be used in a 
return expression, and if so, it feels natural for the type hint to 
propagate:

function foo(): void { do_something(); }
function wrapped_foo(): void { do_something_else(); return foo(); }
// ERROR: can't specify return value in a void function

"return foo()" is not illegal because foo is declared void, but because 
wrapped_foo is - even though the result is exactly as expected.

Regards,
-- 
Rowan Collins
[IMSoP]

---

## #88814 — Marc Bennewitz — 2015-10-15T14:34:16+00:00

URL: https://externals.io/message/88792#88814 · In reply to: #88800

> On 10/14/2015 06:00 PM, Andrea Faulds wrote:  >> Both you and Stas have said this, but it's only true if we solely 
> > […]
other >> languages (these are just off the top of my head), `void` 
functions >> do still have an implicit result. >> >> All of these 
languages would have had the choice to do what you're >> suggesting and 
use `null`, or its equivalent (`undefined` for >> TypeScript and 
ActionScript). They didn't. Why? If I had to guess, >> there's at least 
three reasons. For one, void is the word languages >> usually use for 
this. For another, `void` and `null` they mean >> different things. 
`void` signifies a function isn't returning >> anything. `null` 
signifies a function that *returns null*, regardless >> of where that 
null came from. `function foo(): null { return >> 
some_probably_null_returning_function(); }` should surely be legal >> 
with a `null` type hint, yet it's nonsensical code. Finally, making a >> 
function truly "return nothing", i.e. disallowing its use as an >> 
expression/rvalue, breaks some use cases, like passing along the >> 
result of a callback. >> >> PHP would neither be the first nor the last 
to be using `void` in >> this way. >> >>> If the union types RFC[2] 
passes it >>> makes sense to allow `Foo | null` which allows something 
of type `Foo` >>> or `null`. To me it makes sense that if you then 
remove `Foo` you are >>> left with `null`, not `void`. My personal 
recommendation because of >>> this would be to use `null` for the return 
type and instead of `void`. >> >> `null` would be a weird type, because 
it doesn't make sense as a >> parameter type, and as a return type, you 
don't really want to >> enforce returning null, you want to enforce not 
returning at all (see >> the example above). It feels like a poor man's 
substitute to me. >> >> Thanks. > > The tricky part here is that saying 
a function does not return is not > something PHP currently does: > > 
https://3v4l.org/HtAuC > > No return implicitly returns NULL, which you 
can assign to a variable > if, for some strange reason, you were so 
inclined. So this would be > more than "just" a syntactic documentation 
feature. > > Which I believe gives the following options: > > 1) Change 
the language behavior such that > > function foo() : void { ...} > $a = 
foo(); > > Is a syntax error (because there really was nothing returned 
to > assign), rather than resulting in $a having a value of NULL. > > 2) 
Use null as a "type" (which I agree feels weird just saying it), > such 
that: > > function foo() : null { ...} > $a = foo(); > > and > > 
function foo() { ...} > $a = foo(); > > are identical. The former would 
impact the contents of the function > (eg, a non-empty return would be a 
parse error), but the external > result is the same ($a == NULL). > > 3) 
Use the "void" keyword, but give it the same effect as option 2. > > The 
RFC currently seems to propose option 3 (based on the "Use of void > 
functions in expressions" section). I don't have a strong feeling at > 
this point about which option I'd prefer. >
Option 4)

// implicit return void
function foo () { return; }

// explicit return void
function foo () : void { return; };

// syntax error if returning something on explicit return void
function foo () : void { return null; };

// syntax error on using return value of explicit return void
function foo () : void { return; };
$bar = foo();

// return `NULL` on implicit return void (this could also give a 
warning/notice/deprecated error)
function foo () { return; };
$bar = foo(); // `NULL` 

// mixing return void with any other return values could also result in 
a warning/notice/deprecated error
function foo () { if ($bar) return; return $bar; };

> --Larry Garfield  >

I really like this as in my opinion if a function doesn't return 
something it should be part of the function signature and it really 
helps to avoid mistakes on writing code.

Marc

---

## #88815 — Andrea Faulds — 2015-10-15T15:32:07+00:00

URL: https://externals.io/message/88792#88815 · In reply to: #88810

Hey Chris,

Chris Riley wrote:
> Semantically, I don't believe that there is sufficient difference between
> "returns no value" and "returns a value which has been defined as having no
> value" for us to care about it. The main difference you get between return
> type of null and a return type of void seems to be some extra fatal errors,
> for a developer there seems little value.
>
>  From a user confusion point of view; PHP currently considers return; and
> return null; to be equivalent (in fact some code style checkers will
> replace one with the other) If (and only if) a function has a return type
> of void these two statements are no longer equivalent - one is a fatal
> error one is not. For any other return type specification, return null; and
> return; would behave the same. This in itself would be enough for me to be
> against adding a return type of void.

This is true, but `void` isn't an ordinary return type. It exists to 
ensure a function doesn't return anything, unlike other return types 
which exist to ensure a function returns a specific thing.

`return null;` is *technically* equivalent to `return;`, but it's not 
quite the same in intent, usually. We could special-case it and allow 
it, but why? The return value isn't supposed to be used, why should we 
allow you to specify it? A void function in PHP does technically produce 
a result of null, but it might as well be false or -1 or whatever for 
all we care. Also, if `return null;` is to work, should `return(null);` 
also work? How about `return \null;`? `return SOME_NULL_CONSTANT;`? It 
seems silly to me since, again, the return value is insigificant.

Thanks.
-- 
Andrea Faulds
http://ajf.me/

---

## #88816 — Andrea Faulds — 2015-10-15T15:32:16+00:00

URL: https://externals.io/message/88792#88816 · In reply to: #88812

Hi Rowan,

Rowan Collins wrote:
> I can see the point in denying the right to say "return
> some_function_expected_to_return_null();" But in a sense this is no
> different from declaring that a function returns int, and then writing
> "return some_function_expected_to_return_int();"
>
> If a void function can be used in an expression, it can be used in a
> return expression, and if so, it feels natural for the type hint to
> propagate:
>
> function foo(): void { do_something(); }
> function wrapped_foo(): void { do_something_else(); return foo(); }
> // ERROR: can't specify return value in a void function
>
> "return foo()" is not illegal because foo is declared void, but because
> wrapped_foo is - even though the result is exactly as expected.

Hmm, this is an interesting case you've pointed out. Being able to do 
`return some_other_void_function();` is something I've desired in other 
languages.

But what if that void function you're calling later adds a return value? 
Now the calling function is returning a value other than null, violating 
its type hint and producing a runtime error.

It's a shame there's no "tail call this other function and discard its 
result" construct.

Thanks.


-- 
Andrea Faulds
http://ajf.me/

---

## #88817 — Andrea Faulds — 2015-10-15T15:32:21+00:00

URL: https://externals.io/message/88792#88817 · In reply to: #88809

Hi Rowan,

Rowan Collins wrote:
> But why "invent" (as far as PHP is concerned) this new keyword of "void"
> to mean exactly the same thing "null" already means - absence of a
> definite value?

They don't mean exactly the same thing. null is a value to represent a 
lack of a value. But there's also the concept of *no value at all* (a 
missing variable, say). The closest we have to that in PHP already would 
actually be `unset`, but that's a strange choice given it's not what we 
use in documentation (function prototypes, PHP manual, PHPDoc, etc. use 
`void`), and it's tainted by the bizarro `(unset)` cast which casts to null.

Thanks.

-- 
Andrea Faulds
http://ajf.me/

---

## #88818 — Andrea Faulds — 2015-10-15T15:32:24+00:00

URL: https://externals.io/message/88792#88818 · In reply to: #88803

Hi Larry,

Larry Garfield wrote:
> 
> > […]
>
> Well, that depends on the intent here.  In practice, is the intent to
> add a "no return at all" type of function (ie, $a = foo() is a syntax
> error now where it was not before), or to make it explicit that the only
> legal return is null (even if implicitly that means having a non-early
> return statement is pointless)?  Those are the two options.  That may or
> may not dictate the keyword that gets used.

It would seem ideal to have "true" void functions which can't be used as 
expressions, i.e. producing an error if you try to use them like in your 
example. But they'd cause a backwards-compatibility headache, and I'm 
not sure they're a good idea anyway. Every function call being an 
expression is pretty handy. You can safely capture the return value of a 
callback and pass it along, for instance.

In a sense, what this RFC offers might be called a compromise. It 
enforces the rules of `void` within the function, but at the end of the 
day the caller still gets a null out of it since that's what PHP's 
always done.

> It definitely sounds like you're favoring the second (as that's what the
> RFC says).  Which may or make not make "void" an odd keyword to choose
> when what's actually happening is `NULL` getting returned.  Is `NULL` a
> void?  (There's a deep philosophical question...)

It's an interesting question. There's some precedent for using `void` in 
this way even though the functions aren't "truly" void. I mostly like 
`void` because it's the customary keyword to use, though. Everyone knows 
what a 'void function' is.

> As I said, I don't have a strong opinion on the subject yet.  I'm just
> trying to distil the discussion down to as small a question as possible.
> :-)

I appreciate your efforts. I have a tendency to be a bit verbose in my 
writing, so providing clarity is helpful. :)

Thanks.
-- 
Andrea Faulds
http://ajf.me/

---

## #88819 — Rowan Collins — 2015-10-15T16:00:31+00:00

URL: https://externals.io/message/88792#88819 · In reply to: #88816

Andrea Faulds wrote on 15/10/2015 16:32:
> Hmm, this is an interesting case you've pointed out. Being able to do 
> `return some_other_void_function();` is something I've desired in 
> other languages.
>
> But what if that void function you're calling later adds a return 
> value? Now the calling function is returning a value other than null, 
> violating its type hint and producing a runtime error.

Well, fundamentally, this is true of anything short of full 
type-checking. The type safety below is self-evident:

function foo(): int { return 42; }
function wrapped_foo(): int { return foo(); }

But foo() could later be changed to this:

function foo(): string { return 'The Answer'; }

The wrapped_foo() typehint is now broken. That's not really any 
different from foo() starting off as void/returns-null and adding a 
return value.

In both cases, a static analyser could detect the discrepancy, but the 
Zend Engine will not, until the function is executed.


> It's a shame there's no "tail call this other function and discard its 
> result" construct. 

Yeah, I guess what you really want is for a void function to be able to 
"goto foo();"

Regards,
-- 
Rowan Collins
[IMSoP]

---

## #88820 — Rowan Collins — 2015-10-15T16:09:17+00:00

URL: https://externals.io/message/88792#88820 · In reply to: #88817

Andrea Faulds wrote on 15/10/2015 16:32:
> Hi Rowan,
>
> Rowan Collins wrote:
> > […]
>
> They don't mean exactly the same thing. null is a value to represent a 
> lack of a value. But there's also the concept of *no value at all* (a 
> missing variable, say). The closest we have to that in PHP already 
> would actually be `unset`, but that's a strange choice given it's not 
> what we use in documentation (function prototypes, PHP manual, PHPDoc, 
> etc. use `void`), and it's tainted by the bizarro `(unset)` cast which 
> casts to null. 

Oh, lord, not this discussion again! PHP has no concept of a variable 
being in the "state" of unset; it has programmer hints to warn you if 
you read before writing, and a couple of odd constructs which let you 
access the current symbol table as a dictionary.

The manual has some weird wording in places which implies that a 
variable takes on a type when first used, but in practice the rule is 
much simpler: any variable which hasn't had a value assigned yet is 
considered to have the value NULL, and normal cast rules apply from 
there. The same is true of missing array keys, object properties, 
unset() variables, unspecified return values, etc, etc, etc. They are 
all NULL.

You could add a warning for "function foo() { return; } $a = foo();" to 
match "$x = []; $a = $x['foo'];", but $a would still unambiguously 
contain the value `NULL` in both cases, and outside of some odd 
applications like templating, it is *values* the program should be 
concerned with, not the *reason* for those values.

Regards,
-- 
Rowan Collins
[IMSoP]

---

## #88821 — Pedro Cordeiro — 2015-10-15T16:14:15+00:00

URL: https://externals.io/message/88792#88821 · In reply to: #88819

I've been thinking about what PHP should do when accessing the return value
of a `void` function, and so far, I think the consistent thing should be to
get NULL, while throwing an E_NOTICE.

$a = $b; //$b wasn't initiated.

This does the same thing. I tried accessing what was supposed to be
'nothing' ($b), and got `NULL` with an `E_NOTICE` being thrown.

function myFunc() : void { ... }
$a = myFunc();

I see no reason why this should silently assign `NULL` to $a, *specially* with
"return null;" being explicitly forbidden.

My point is: we already use an implicit `NULL` return on functions with
missing return values (or with "return ;"), but now we're explicitly
prohibiting even a `NULL` return. The behaviour should be consistent (NULL
should be accessed, as for all undefined things), with an E_NOTICE.

function myFunc() { return ; }
$a = myFunc();

Should assign `NULL` to $a and *not* throw an `E_NOTICE` (this is the current
status), because a return type was not forbidden with the keyword 'void'.

2015-10-15 13:00 GMT-03:00 Rowan Collins <rowan.collins@gmail.com>:

> Andrea Faulds wrote on 15/10/2015 16:32:
>
> > […]
>
> Well, fundamentally, this is true of anything short of full type-checking.
> The type safety below is self-evident:
>
> function foo(): int { return 42; }
> function wrapped_foo(): int { return foo(); }
>
> But foo() could later be changed to this:
>
> function foo(): string { return 'The Answer'; }
>
> The wrapped_foo() typehint is now broken. That's not really any different
> from foo() starting off as void/returns-null and adding a return value.
>
> In both cases, a static analyser could detect the discrepancy, but the
> Zend Engine will not, until the function is executed.
>
>
> It's a shame there's no "tail call this other function and discard its
> > […]
>
> Yeah, I guess what you really want is for a void function to be able to
> "goto foo();"
>
> Regards,
> --
> Rowan Collins
> [IMSoP]

---

## #88822 — Chris Riley — 2015-10-15T16:20:49+00:00

URL: https://externals.io/message/88792#88822 · In reply to: #88818

> In a sense, what this RFC offers might be called a compromise. It enforces
> the rules of `void` within the function, but at the end of the day the
> caller still gets a null out of it since that's what PHP's always done.
>
>
>
If this truly is the case, then all you get over hinting void as the return
type instead of null is a fatal error for return null; Sure you can argue
for capturing intent but realistically the difference in intent between
'returns no value' and 'does not return a value' is something for
philosophers to argue over not programmers. ;)

---

## #88823 — Rowan Collins — 2015-10-15T16:21:28+00:00

URL: https://externals.io/message/88792#88823 · In reply to: #88821

Pedro Cordeiro wrote on 15/10/2015 17:14:
> I've been thinking about what PHP should do when accessing the return 
> value of a `void` function, and so far, I think the consistent thing 
> should be to get NULL, while throwing an E_NOTICE.
>
> $a = $b; //$b wasn't initiated.
>
> This does the same thing. I tried accessing what was supposed to be 
> 'nothing' ($b), and got `NULL` with an `E_NOTICE` being thrown.
>
> function myFunc() : void { ... }
> $a = myFunc();
>
> I see no reason why this should silently assign `NULL` to $a, 
> *specially* with "return null;" being explicitly forbidden. ... The 
> behaviour should be consistent (NULL should be accessed, as for all 
> undefined things), with an E_NOTICE.

I don't know how easy this would be to implement, but I agree that this 
would make the "void" keyword feel more meaningful.

Does the Engine have a way to know whether a return value is being used 
or discarded for optimisation purposes? If so, could that logic be 
hooked to provide the Notice?

Regards,
-- 
Rowan Collins
[IMSoP]

---

## #88824 — Andrea Faulds — 2015-10-15T16:34:16+00:00

URL: https://externals.io/message/88792#88824 · In reply to: #88821

Hi Pedro,

Pedro Cordeiro wrote:
> I've been thinking about what PHP should do when accessing the return value
> of a `void` function, and so far, I think the consistent thing should be to
> get NULL, while throwing an E_NOTICE.

We could do this, but I do wonder if it might cause a lot of E_NOTICEs 
to pop up for existing code, assuming we applied this to built-in PHP 
functions.

I'm not sure.

Thanks.
-- 
Andrea Faulds
http://ajf.me/

---

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