On Sun, 3 Feb 2019 at 18:39, David Rodrigues <david.pro...@gmail.com> wrote:
>
> Hello everybody!
>
> The biggest problem that I see about this topic is the loss of performance,

Although performance impact might be a problem, my impression is that
PHP has some  problems supporting method overloading that don't exist
in other languages.

> So, what other languages does in cases like
> that? Java, for instance.

Java doesn't have type-juggling so they avoid quite a few problems
with dynamic types.

Marcos Passos wrote:

> Java, for instance, throw a compile-time error saying that the
> method signature is ambiguous.

Java compiles all the code in a program before running it, which makes
that solution possible. PHP doesn't and so has to make some decisions
at run-time.


As Christoph wrote:

> overload function sum(int $a, int $b): int;
> overload function sum(float $b, float $b): float;
>
> > Which function would sum(17.4, 42) call?

That's the fundamental problem that would need addressing.

I believe that to have a chance of passing, any method overloading RFC
would need to have a really comprehensive answer that covers all of
the edge cases.

But in addition to that, a really strong argument for why method
overloading is a good thing needs to be made as well, particularly as
the equivalent behaviour can be done in userland*.

btw It is possible, that the thing mostly likely to happen, and
provide the most benefit for you, would be support in your IDE for
autocompletion as implemented in userland. Which might take a lot less
effort than implmenting the feature in PHP core...

cheers
Dan
Ack


* userland implementation:

/**
 * @method sum(int $a, int $b): int;
 * @method sum(float $b, float $b): float;
 */
class Foo
{
    public function __call($name, $arguments)
    {
        if ($name === 'sum' && is_int($arguments[0]) && is_int($arguments[1])) {
            return $this->sumInts($arguments[0], $arguments[1]);
        }
        if ($name === 'sum' && is_float($arguments[0]) &&
is_float($arguments[1])) {
            return $this->sumFloats($arguments[0], $arguments[1]);
        }
        throw new \InvalidArgumentException("Don't know how to call this");
    }

    private function sumInts(int $a, int $b): int
    {
        // echo "sumInts was called.\n";
        return $a + $b;
    }

    private function sumFloats(int $a, int $b): int
    {
        // echo "sumFloats was called.\n";
        return $a + $b;
    }
}


$foo = new Foo();

$foo->sum(4, 4);
$foo->sum(4.1, 4.1);

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

Reply via email to