Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > de.comp.lang.php > #3855

Re: return types

Path csiph.com!weretis.net!feeder4.news.weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail
From "Christoph M. Becker" <cmbecker69@arcor.de>
Newsgroups de.comp.lang.php
Subject Re: return types
Date Sat, 2 Apr 2016 19:23:24 +0200
Organization solani.org
Lines 55
Message-ID <ndov5m$hei$1@solani.org> (permalink)
References <1t56fced94i14b9n3e8%sfroehli@Froehlich.Priv.at> <3243755.WBM996tjSl@PointedEars.de> <dm7lpsFpt1jU1@mid.individual.net>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
X-Trace solani.org 1459617782 17874 eJwNycERwDAIA7CVMGC4jgO+ZP8RGn3FKJQ6i5W8vLGdjvDxWUwKuGFurw4OY+lDcdpW+kz1Awy9EM8= (2 Apr 2016 17:23:02 GMT)
X-Complaints-To abuse@news.solani.org
NNTP-Posting-Date Sat, 2 Apr 2016 17:23:02 +0000 (UTC)
User-Agent Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.7.0
X-User-ID eJwFwYEBwCAIA7CXRNrCzhEm/59gQpepA6LA4ezTawfrjgXuyimWCcfCIXxqxb+b426GznwUphBv
In-Reply-To <dm7lpsFpt1jU1@mid.individual.net>
Cancel-Lock sha1:8Zlf+AASXHWpXnT8IIbDCq0MK7k=
X-NNTP-Posting-Host eJwFwYEBgDAIA7CXKFDEc3Cj/59gwijUebJYSVG9M6Ty3gFC+CLpOy0/bRtw3FjhpZkw0z8orRFG
Xref csiph.com de.comp.lang.php:3855

Show key headers only | View raw


Stefan Froehlich schrieb:

> Ich persönlich möchte, wenn ich lese:
> 
> #v+
> abstract class Foo {
> 	public function bar(int $b) : int;
> }
> #v-
> 
> ...auch *garantiert* das folgende fehlerfrei tun können:
> 
> #v+
> if ($f instanceof Foo) $b2 = $f->bar($b1);
> #v-
> 
> ...und nicht womöglich daran scheitern, dass die abgeleitete Klasse
> Foo_Bar die Methode womöglich ganz anders definiert hat.

Da geht es Dir genau wie Barbara Liskov, die daher das nach ihr benannte
Substitutionsprinzip formuliert hat. Und mir geht es auch so. :)

> Allerdings würde ich mir
> gelegentlich das wünschen, was ich bei C++ so geschätzt habe, z.B.:
> 
> #v+
> abstract class Foo {
> 	public function bar(Array $a) : int;
> 	public function bar(Collection $c) : int;
> }
> #v-

Das ist allerdings keine dynamische Polymorphie, sondern Überladung
(overloading), und hat, streng genommen, nichts mit Objektorientierung
zu tun. Eine solche Überladung ist wohl bei dynamischen Sprachen eher
ungewöhnlich, weil sie nicht einfach zu implementieren ist (ganz im Ggs.
zu statisch kompilierten Sprachen).

> So, wie es derzeit implementiert ist, ist das aber jedenfalls
> halbgar und hilft weder Dir noch mir weiter.

Rückgabetyp-Invarianz bzgl. der Funktions-Definition ist aber doch (noch
immer) ziemlich verbreitet, oder? Kovarianz wäre natürlich
wünschenswert, erfordert aber zumindest, dass die Rückgabetypen zur
Kompilationszeit bereits bekannt sind, was u.U. gar nicht so einfach zu
lösen ist. Derzeit ist das PHP jedenfalls nicht nötig. Der folgende Code
wird anstandslos akzeptiert:

  class Foo
  {
      function bar(): Baz {}
  }

-- 
Christoph M. Becker

Back to de.comp.lang.php | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

return types Stefan+Usenet@Froehlich.Priv.at (Stefan Froehlich) - 2016-03-31 09:35 +0000
  Re: return types k@rl.pflaesterer.de (Karl Pflästerer) - 2016-03-31 12:42 +0200
  Re: return types "Christoph M. Becker" <cmbecker69@arcor.de> - 2016-03-31 14:16 +0200
    Re: return types Stefan+Usenet@Froehlich.Priv.at (Stefan Froehlich) - 2016-04-15 06:37 +0000
    Re: return types Stefan+Usenet@Froehlich.Priv.at (Stefan Froehlich) - 2016-06-22 08:48 +0000
      Re: return types "Christoph M. Becker" <cmbecker69@arcor.de> - 2016-06-22 12:25 +0200
        Re: return types Stefan+Usenet@Froehlich.Priv.at (Stefan Froehlich) - 2016-06-22 14:49 +0000
          Re: return types "Christoph M. Becker" <cmbecker69@arcor.de> - 2016-06-22 17:47 +0200
            Re: return types Stefan+Usenet@Froehlich.Priv.at (Stefan Froehlich) - 2016-06-23 08:23 +0000
              Re: return types "Christoph M. Becker" <cmbecker69@arcor.de> - 2016-07-02 01:03 +0200
  Re: return types Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-03-31 21:40 +0200
    Re: return types Stefan+Usenet@Froehlich.Priv.at (Stefan Froehlich) - 2016-04-01 16:26 +0000
      Re: return types "Christoph M. Becker" <cmbecker69@arcor.de> - 2016-04-02 19:23 +0200
        Re: return types Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-04-02 20:36 +0200
        Re: return types Stefan+Usenet@Froehlich.Priv.at (Stefan Froehlich) - 2016-04-11 21:18 +0000
      Re: return types Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-04-02 20:33 +0200
        Re: return types Stefan+Usenet@Froehlich.Priv.at (Stefan Froehlich) - 2016-04-11 21:32 +0000
          Re: return types Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-04-19 20:24 +0200
            Re: return types Stefan+Usenet@Froehlich.Priv.at (Stefan Froehlich) - 2016-04-20 07:05 +0000
              Re: return types Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-04-21 02:55 +0200

csiph-web