On 11.04.24 03:03, Levi Morrison wrote:
IMO, it's better to separate it into two functions because its type is
stable without control flow. For instance:

     // Returns K if $b is true, V otherwise.
     function array_find(
         array<K, V> $array,
         callable(V, K): bool $callback,
         bool $b = false
     ) -> K|V;

This isn't stable and requires control-flow to understand the type.
These are simpler:

     function array_find(
         array<K, V> $array,
         callable(V, K): bool $callback
     ) -> V;

     function array_find_key(
         array<K, V> $array,
         callable(V, K): bool $callback
     ) -> K;

Naming bikeshedding aside, it's better to have types that are
inferrable without function-specific knowledge of control flow. It
doesn't matter if it's a bool or an enum, it still has problems.
Better to just separate them to different functions.

I definitely see the point where there is an advantage to having two separate methods and can definitely understand that it is easier for developers to understand the control flow without evaluating the parameters.

I'm unsure if that's really necessary though, because basically it's probably not necessary to directly see what exactly the function returns. Perhaps there will be another opinion on this in an email in the next few days.

Cheers

Josh

Reply via email to