> -----Ursprüngliche Nachricht-----
> Von: Nikita Nefedov [mailto:inefe...@gmail.com]
> Gesendet: Donnerstag, 12. Februar 2015 15:54
> An: Andrea Faulds
> Cc: Pavel Kouřil; PHP Internals
> Betreff: Re: [PHP-DEV] [VOTE] Scalar Type Hints
> 
> Hi,
> 
> 2015-02-12 18:31 GMT+04:00 Andrea Faulds <a...@ajf.me>:
> 
> > Hi Pavel,
> >
> > > On 12 Feb 2015, at 13:48, Pavel Kouřil <pajou...@gmail.com> wrote:
> > >
> > > C# does have dynamic typing.
> >
> > No it doesn’t, it’s a statically-typed language. I don’t understand
> > why you say it has dynamic typing - there is some limited dynamism in
> > parts, but I don’t think it affects what we’re talking about. Dynamic
> > typing and polymorphism aren’t the same.
> >
> 
>  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".
> 
> 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).
> 
> [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



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

Reply via email to