On Thu, Feb 12, 2015 at 12:44 PM, Andrea Faulds <a...@ajf.me> wrote: > > PHP already has polymorphism through its dynamic typing. I don’t see method > overloading happening any time soon. We have optional parameters and dynamic > typing, which all but eliminate the main needs for overloading. We’re a > weakly-typed language, so overloading on scalar types might cause > unpredictable behaviour (with strict types you’d get an error sometimes, and > that’s fine - but calling an entirely different function? Now that’s > different.) There’s also the question of how, or if at all, method > overloading could interact with our optional parameters and dynamic typing. > Finally, overloading can lead to really poor API design: it’s not that > uncommon in some languages to see 20 different function definitions with > overlapping and non-comprehensive subsets of a method’s behaviour, that are > distinguished only by parameter types. It’s one of my least favourite > Java/C#/C++ features for that reason. > > In response to saying numbers are not a type: well, they are more of an > abstract type which integer and float subclass. We already have internal > facilities for converting to and from “numbers” alongside integers and > floats, I don’t see why we can’t expose this to users. >
Hello, I don't see any problem with combination of optional parameters with method overloading. C# has it and it is pretty useful and works there without any bigger issues. Sure, the dynamic typing may be a (small) issue, but again, C# does it in an IMHO meaningful way as well and it can be used as an inspiration, should this way be chosen. Overloading on scalar types - unpredictable how? Even with weak calls, the best candidate could be chosen? Of course, sometimes there will be an abmbiguous call, but as long as there would be a way to define which call you want, it shouldn't be a problem as well? Poor API - for useland code or in PHP's functions itself? But I don't think this should be considered as a real problem, because in userland code, developers are able to create a poor API even with the current versions of PHP. If someone wants to shoot himself in the foot, he will do it in one way or another. And at the same time, polymorphism via method overloading can lead to a cleaner APIs, if it's used well. Well, about "number" type hint. How this would work, in combination with strict types? function bar(float $f) { return $f * 1.23; } function foo(number $x) { return bar($x); } foo(1); >From my understanding, it wouldn't? Regards Pavel Kouril -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php