Re: [RFC] Nullable Return Type Declaration

From: Date: Fri, 15 Apr 2016 17:12:02 +0000
Subject: Re: [RFC] Nullable Return Type Declaration
References: 1 2 3 4 5 6 7  Groups: php.internals 
Request: Send a blank email to internals+get-92343@lists.php.net to get a copy of this message
On 4/15/16 12:22 AM, Levi Morrison wrote:
My point is that foo(bar(), $val) won't die because bar may return null. Bar is expected to return null sometimes. For example, let's consider an administrator page where they look up user information based on an identifier. The routine we'll use will have this signature:
     function get_user(string $id): User | Null;
It is possible for an identifier to not exist and this is not an error (database successfully returned no results). If there is no User data to display then it makes sense for the UI to present that differently. Thus it makes sense to pass that User | Null onto the code that will present it:
     $user_data = get_user($id);
     // ...
     $user_html = render_user_data($user_data);
In fact this is a common operation that is encountered in many code bases (I think every single one I've ever looked at).
This is a good example. My opinion is that *because* get_user() can return null (a red flag) I prefer to see explicit handing of the null case before doing anything else. If I would end up with render_user_data(get_user($id)) I would consider it fair to not hint the param because I didn't earn it. I invented the faux docblock tag @sorry for this kind of thing. Tom

« previous php.internals (#92343) next »