Re: Re: PHP 8: Method Overloading, The Ressurection

From: Date: Sun, 03 Feb 2019 19:19:33 +0000
Subject: Re: Re: PHP 8: Method Overloading, The Ressurection
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-104080@lists.php.net to get a copy of this message
On 03/02/2019 19:00, Christoph M. Becker wrote:
On 03.02.2019 at 19:39, David Rodrigues 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? Also consider: sum(PHP_INT_MAX, PHP_INT_MAX) vs. sum(PHP_INT_MAX+1, PHP_INT_MAX+1)
Yes, marking overloaded functions explicitly definitely helps, but I think the dispatch part is more complex than it first seems. Classes and interfaces require a bit of subtlety too: class Foo {} class Bar extends Foo {} overload function foo(Foo $a, Foo $b); overload function foo(Foo $a, Bar $b); overload function foo(Bar $a, Foo $b); foo(new Bar, new Bar); There are plenty of cases more complex than this, e.g. when a class implements multiple interfaces, and you need to find a matching signature. That's not to say it's impossible, just don't underestimate the edge cases you'll need to legislate for. Oh, and note that return types can't participate in overloading, because there's no concept of "desired type" to choose between them. Regards, -- Rowan Collins [IMSoP]

« previous php.internals (#104080) next »