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]


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

Reply via email to