Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:110500 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 89978 invoked from network); 12 Jun 2020 16:11:15 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 12 Jun 2020 16:11:15 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id D03601804CB for ; Fri, 12 Jun 2020 07:55:40 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-ot1-f42.google.com (mail-ot1-f42.google.com [209.85.210.42]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 12 Jun 2020 07:55:37 -0700 (PDT) Received: by mail-ot1-f42.google.com with SMTP id 97so7559611otg.3 for ; Fri, 12 Jun 2020 07:55:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=QpajQAuxnZzMLKBj0s35nbSumXV+TyalxgXfTElsdxs=; b=TKSvgO90kU6lT/TLC7Bd62OetQsbx4Atk5R71J/Fe8SNAoJgV+Vfm2L8rxhyv4LvH9 p+ejNhMZHMzKy4Dap1Cp6VVzJV+Ll2pDRrDMY+uuOx71a5H7ufv6P3VVou1xHfyjBw3Y 7KzAgHl1yB5NIxUqWjdKsWZZa6GdT2DImnFdki+7pfQ7kJ01d4kAM6vsyLrpKymaIBQR HDKbagq96v3fQehQ0oTIjnytec+B2xazaG3jIMialnZ7u0zU7aj5xWoeWnuqqUhwrFjh G1k9Jn1Xdf0PJbvRMIQa/YK0NYtdVaRt5qe3val+gmacDCBrRefHeN/k5/a4SRos2Su0 6U7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=QpajQAuxnZzMLKBj0s35nbSumXV+TyalxgXfTElsdxs=; b=E5cOs0kdoKvmkYN9ePiTYDA9ANazYA8V+vzpdN3B4FT0lIzVT/1SyRa8VqABRER6lw GWRNLfrPNQ3/KuMjeO2lnIIQd7qryDDb71nH/jhyBktZhOVpBygnMm3dO2UBao8ELz3y xnG6JQM1HR9SZtsQ0CGfrgwjMCnQvN1GcaHXNZ1kfKdM3nmA4HMgWc80ma3Ggp6ebyuR ypJa5KLDGupwfJaWG7rTZjTbzMrnSZDboVoknrLKdD9hPlIG98wzPuq9n2mAMIgdRIPu al1mOCVHfSbv4co2Y0vGV0uz1OnPiLhut2oll2w2XmreLIJWc3SjU0QMZcIEVdS5Op1f dUAQ== X-Gm-Message-State: AOAM532gfJti3O9WK8w1wu8oWda5q4SOWxBRgjkC9vMKroCqcerAZB60 8XXQ1xtOZQmKNQ9ro2C3Fhcc6dpl/Wx33GkNNMGO7Q== X-Google-Smtp-Source: ABdhPJy36vcSWcT+3R1YpOq7D6z1T5o2qMEjwpZkbr7fYWZMkbYPk89+bcA4A5qipZwj0YgTXyORlL2+wqtZnYxXpbw= X-Received: by 2002:a9d:c29:: with SMTP id 38mr10472385otr.107.1591973736486; Fri, 12 Jun 2020 07:55:36 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Fri, 12 Jun 2020 09:55:24 -0500 Message-ID: To: PHP internals Content-Type: multipart/alternative; boundary="0000000000000e197505a7e44510" Subject: Re: [PHP-DEV] RFC: separate inheritance from subtyping (PHP v8) From: tendoaki@gmail.com (Michael Morris) --0000000000000e197505a7e44510 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable PHP's notion of "implements" is identical to that of Java and C#. This is not by accident, many programmers have to work in multiple programming languages. The less a language does things "differently" the better. And honestly, the upsides of this proposal are not worth the BC breaks created with the new keywords. On Wed, Jun 10, 2020 at 11:16 AM Doug Wilbourne wrote: > I propose the following language additions: > > > > 1) add three new keywords to the available syntax for class declarations: > 'inherits' and 'subtypes' and 'patterns'. > > > > Currently the 'extends' keyword means both inherits and subtypes, > which leads to unnecessarily restrictive typing problems in order to > satisfy the Liskov substitution principle. 'Inherits' would mean that > class A inherits B's properties and methods but is not a subtype of B and > cannot be substituted for B. And just as A can override/overload B's > methods, the method signatures would not be subject to the parameter > widening / return-type restricting principle necessary for substitution. > Conversely, the 'subtypes' keyword indicates that if A subtypes B then A > can be substituted anywhere B can be used but 'subtypes' does not imply > that A inherits any of B's methods and properties - simply that it has > signatures that conform to the parameter widening and return-type > restricting principles of substitution. Lastly 'patterns' complements th= e > 'implements' keyword. A class 'patterns' an interface if it conforms to > the declared signature of the interface but it does not imply that the > concrete class can be substituted for any other object which either > implements or patterns the interface. > > > > It should be possible to write 'A inherits, subtypes B' and this should > have the same effect as 'A extends B'. > > > > 2) Add two new operators: 'descendentof' and 'subtypeof' that would augme= nt > 'instanceof'. Semantics parallel the class declaration keywords describe= d > above. > > > > > > Benefits: > > > > It would then be possible to write something like the following: > > > > interface ValidatorInterface > > { > > public function validate($x) : bool; > > public function getErrmsg() : string; > > } > > > > abstract class Validator patterns ValidatorInterface > > { > > abstract public function validate($x) : bool; > > > > public function getErrmsg() : string > > { > > return $this->errmsg; > > } > > } > > > > class ValidatorNonNegativeInteger inherits Validator patterns > ValidatorInterface > > { > > public function validate(int $x) : bool { return 0 <=3D $x; } > > } > > > > class ValidatorAlphaText(string $x) inherits Validator patterns > ValidatorInterface : bool > > { > > return 1 =3D=3D preg_match('/^[a-zA-Z]+$/', $x); > > } > > > > ValidatorNonNegativeInteger and ValidatorAlphaText cannot be substituted > for each other or Validator or ValidatorInterface. Explicitly trying to > upcast ValidatorNonNegativeInteger should produce a compiler error (e.g. > (Validator) new ValidatorNonNegativeInteger() is not legal). > > > > If you adopt the data type 'mixed' and use it as in "public function > validate(mixed $x)", it makes it even clearer that the syntax permits > covariant parameters. > > > > Currently the compiler allows contradictory parameter declaration between > parent and descendant (using 'extends') but produces a runtime error > indicating that the child method's parameter must conform to that of the > parent. Because substitution is not an issue when A inherits B (using > =E2=80=98inherits=E2=80=99 as above, not =E2=80=98extends=E2=80=99), a pa= rent declaring "function foo(int > $bar)" and a descendant declaring "function foo(string $bar)" has no > theoretical problem as far as I can tell and should not produce an error. > > > > Along with generics (already being suggested / discussed I think - and > thank you for unions), this approach should lead to DRYer code and cleane= r > abstractions. In the example above, ValidatorNonNegativeInteger and > ValidatorAlphaText can leverage common code in the parent class. And it = is > not necessary to write two separate interfaces in order to handle the two > different data types. Essentially, it creates a bridge between the old > 'untyped' Zvals and the tightly typed C derivatives. > > > > > > Who Is Going To Write This? > > > > I am relatively new to the PHP community and have never once glanced at t= he > internals of PHP. As much as I would love to do it, my learning curve wi= ll > be quite some time before I would be ready to tackle something like this. > And I think the time for this idea is now while there is so much momentum > in the community on type-safety. I am considering getting involved in th= e > community per the guidance (test writing, documentation, etc) but have no= t > plucked up the courage to make the commitment yet. And my C is super > rusty. If this falls on deaf ears, I am afraid the idea will never bear > fruit. If everyone thinks it is a good idea, then I am particularly sorry= I > cannot do it myself since it is wrong to make my problem someone else=E2= =80=99s > problem. I would consider writing something in PHP to implement the idea= , > but cannot really see how it could be accomplished. > > > > Thank you for your time and consideration. And thank you so much for all > the work you do to make PHP the product that it is. > > > > Kind regards, > > > Doug Wilbourne > --0000000000000e197505a7e44510--