On Thu, Mar 11, 2021, at 6:10 AM, Hamza Ahmad wrote:
> Hi Victor,
> 
> It does not contradict because it does not cover two different aspects. 
> First, return types specify the types of to be returned values. Second, 
> this request proposes a shorthand technique to avoid writing a single 
> line code multiple times. Third, it is similar to default argument 
> values that are also typed.
> 
> Cheers!

Greetings.  First, please don't top post.  This is a bottom-posting list.

As to your proposal, it's a bit hard to follow because it sounds like you're 
suggesting an implicit "return of nothing" built on union types, when based on 
your follow ups what I think you're suggesting is something more like this:

function foo(int $bar = 4): int = 0 {
  if (is_even($bar)) {
    return; // Implicitly returns 0
  }
  return $bar * 2;
}

That is, allowing a default return value.  Union types aren't part of the 
picture.

I can see how you got to the idea, but honestly I am not a fan.  Primarily... 
I've never run into the situation you describe where multiple early-returns 
would have the same value.  Perhaps it's just because in those cases I end up 
combining the conditionals with an ||, but I don't see the problem you 
describe.  Different code paths should result in different results, or else why 
are they different code paths?

So, code that's doing that I'd be suspicious of to begin with.

Even if you did end up in such a situation, since you're dealing with a static 
value, setting the default internally within the function is trivial:

function foo(int $bar = 4): int {
  $default = 0;
  if (is_even($bar)) {
    return $default;
  }
  return $bar * 2;
}

So basically, in the few cases where this isn't a code smell, it's unnecessary.

--Larry Garfield

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to