Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:82543 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 2624 invoked from network); 12 Feb 2015 16:15:35 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 12 Feb 2015 16:15:35 -0000 Authentication-Results: pb1.pair.com smtp.mail=php@tutteli.ch; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=php@tutteli.ch; sender-id=pass Received-SPF: pass (pb1.pair.com: domain tutteli.ch designates 80.74.154.78 as permitted sender) X-PHP-List-Original-Sender: php@tutteli.ch X-Host-Fingerprint: 80.74.154.78 ns73.kreativmedia.ch Linux 2.6 Received: from [80.74.154.78] ([80.74.154.78:39002] helo=hyperion.kreativmedia.ch) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 36/49-50473-4A1DCD45 for ; Thu, 12 Feb 2015 11:15:34 -0500 Received: (qmail 32053 invoked from network); 12 Feb 2015 17:15:29 +0100 Received: from cm135-167.liwest.at (HELO RoLaptop) (81.10.135.167) by ns73.kreativmedia.ch with ESMTPSA (AES256-SHA encrypted, authenticated); 12 Feb 2015 17:15:25 +0100 To: "'Nikita Nefedov'" , "'Andrea Faulds'" Cc: =?utf-8?Q?'Pavel_Kou=C5=99il'?= , "'PHP Internals'" References: <8703B53E-2C4A-4AC6-95C4-D4F19C6D5221@ajf.me> <54DAF884.7000508@lerdorf.com> <203e611c8e0b03568a868b8d931aec37@mail.gmail.com> <61E01A7C-C5C8-447F-A8FA-E12A18E847CA@ajf.me> <85D76C91-9A9E-459B-847A-619AA21B9262@ajf.me> <342635C4-F0EA-4557-A37F-753E51100402@ajf.me> In-Reply-To: Date: Thu, 12 Feb 2015 17:15:24 +0100 Message-ID: <007b01d046df$1bde01d0$539a0570$@tutteli.ch> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Mailer: Microsoft Outlook 14.0 Thread-Index: AQJMIZG9mXkdirFXmuP9r5ZK3xIuzQJbJJA/AzCIQggCYWllLQKBurkXAYsw6s4B6qYCMgHjX/2GAb5UKOABwylnMQKF/fUwArGIQ84B1RtadQMO5Q+CAqJubzkBDVl/xZrsxJ+Q Content-Language: de-ch Subject: AW: [PHP-DEV] [VOTE] Scalar Type Hints From: php@tutteli.ch ("Robert Stoll") > -----Urspr=C3=BCngliche Nachricht----- > Von: Nikita Nefedov [mailto:inefedor@gmail.com] > Gesendet: Donnerstag, 12. Februar 2015 15:54 > An: Andrea Faulds > Cc: Pavel Kou=C5=99il; PHP Internals > Betreff: Re: [PHP-DEV] [VOTE] Scalar Type Hints >=20 > Hi, >=20 > 2015-02-12 18:31 GMT+04:00 Andrea Faulds : >=20 > > Hi Pavel, > > > > > On 12 Feb 2015, at 13:48, Pavel Kou=C5=99il = wrote: > > > > > > C# does have dynamic typing. > > > > No it doesn=E2=80=99t, it=E2=80=99s a statically-typed language. I = don=E2=80=99t understand > > why you say it has dynamic typing - there is some limited dynamism = in > > parts, but I don=E2=80=99t think it affects what we=E2=80=99re = talking about. Dynamic > > typing and polymorphism aren=E2=80=99t the same. > > >=20 > C# actually supports dynamic typing with a dynamic keyword [1]. But = it really doesn't mean that having method > overloading in a dynamic language is a good idea... C#, although it = has a full ability to support dynamic typing, is not usually > used in this fashion. To understand why it's not the best idea to use = dynamic types with overloading you can just google > "C# dynamic type with overloading site:stackoverflow.com". >=20 > Another great deal in this question is performance, I think this = subject was brought up a plenty of times in this ML and it > was pointed out a couple of times that overloading even if doable at = some point, would harm performance of method calls, > which are already one of the slowest (if not the slowest) OPs in the = engine. Languages like C# are usually able to resolve > method references at compile time (unless virtual methods are used, = even then it's only a matter of choosing right method > from hierarchy not from all the overloaded variants). >=20 > [1] https://msdn.microsoft.com/en-us/library/dd264736.aspx There are several programming languages which do support dynamic typing = and method overloading somehow (Clojure, Erlang, Prolog, Cecil and = certainly more). Most of them use a multi-method approach and I think = if PHP would introduce function/method overloading, then such an = approach would be appropriate. Right now, I need to implement the = dynamic dispatching quasi myself which might be more verbose but is also = more cumbersome (and uglier IMO). Consider the following (fictional = example): I want to write a Logger-Service which provides one public method "log" = which writes all kind of objects to a log. The corresponding classes do = not all belong to my code base, are part of third party libraries = respectively, so I am not able to introduce some interface which all = classes implement. The strategy pattern is certainly a good idea for = this problem but nevertheless, somewhere I need to have the distinction = based on many if/else with instanceof (latest in the = LoggerStrategyFactory) -- off topic, if someone has a better design = approach to handle this problem, then let me know it in a private = message, would be interesting as well ;) class Logger{ public function log($x){ if($x instanceof Foo){ logFoo($x); }else if($x instanceof Bar){ logBar($x); } //... }else{ throw new Exception("type ".gettype($x)." not supported"); } } private function logFoo(Foo $f){ //.. } private function logBar(Bar $b){ //.. } //... } With method overloading I could write the following, removing the hassle = to write the dynamic dispatch myself: class Logger{ public log(){ $this->_log($x); } private function _log(Foo $f){ //.. } private function _log(Bar $b){ //.. } //... private function _log($x){ throw new Exception("type ".gettype($x)." not supported"); } } Which is cleaner IMO. Cheers, Robert