Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122953 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id 299D91A009D for ; Thu, 4 Apr 2024 22:37:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1712270266; bh=XF5wNYt3Ub0Icd9FKOqCrOoRrf0x1/WrWKNnYQ7hkNM=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=I0+Wd904/IIhIe+FDB71H4hnBZGBa+ZP6iFnEwb5K1jM3PZyovu4XmlxddXSRRfWl QcYEKJAcuECanv5o/zzS7xxnlf4oF9XtnGGVeMSyvTwzBzhxsKUI3fXc9zFIybCuFC uOoTpMZfh0sus8Dql2KY6txKH6+O1W9gxtuFfDGkqcCw+DVUO8JxiO5p/EId4tl27J NhZDbtdlVM3PhbyQ1+O0TGXEmPjdy/xcI/fh6tDz/WHc8TUPuumtkI0NdzZ79NcZyJ STBYSf2bhKh13jb5kz5z5Jao42zx0pmvSdFG7QeOuTh7CVYSQuOFhkFjUhhkyEnTSV TdrQZkJpzWZUw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 59B381807B3 for ; Thu, 4 Apr 2024 22:37:43 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-pj1-f43.google.com (mail-pj1-f43.google.com [209.85.216.43]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 4 Apr 2024 22:37:40 +0000 (UTC) Received: by mail-pj1-f43.google.com with SMTP id 98e67ed59e1d1-2a074187a42so1129102a91.0 for ; Thu, 04 Apr 2024 15:37:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712270230; x=1712875030; darn=lists.php.net; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=eoUVpiltilvyj5yz5WXHf7b9OWhr0+a/5sflHYk2BnE=; b=mx49iKcOcImrEVliSTPhATlBQdwO05Dea70q4/xUw1kbp4BpvSHlvhMsF/O4xCMAqB BAaPx28eoB3eqJvwaVQ2N0tlJd0gJOkOWu4XGh15lIpWbWWILWAqf7pk3lDcM3sXbYnZ ZFN+swtTS7Wc/skt3Hlv+uEe3vaoeEh4VlvaOZbj0e3zpeh6NgcTMjEvpVi/zterbazf BWehJ1iZv21KLfEM85lw9T0YFcML9a5W+4P2xVc/rtsem96/yPWlpDQzfGOcUfETxyYJ 5VdFPczCgIXpiRpkJHu/O+sNvFo95ewwWhHvpfhH2L6pjO+Hnz4t3DqRJ1MqiQ1/+u/7 SpVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712270230; x=1712875030; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=eoUVpiltilvyj5yz5WXHf7b9OWhr0+a/5sflHYk2BnE=; b=YImezJWl9Y7uKNYNxluFBqv+1rBUSawPB2TCqjQACBzlR7niglV7WVT/awep4DE8gN 4o1lsOV7j9a6n6O2LpJEMXUz1ODfnOnpAIdvd+4ke3gcHiDPgE/V5286yC6nWZ2Tyb9K 16XC0cV3iiF26V66bY7u+KQH2ejGsqY1AVpBMzKUxJYu4d7R+cf52mRrE/w2NFYZoHif ea4yDUF/FVFK1HLJfEIPTocH70pRn5Nr51xo06H7ZuadYTBIvIpduNAN6FrPyAXru6ay IpbvhComHnLkiI0K5RRCASruy9cjh9SImZTFFpUBBvAboVHPs9Gh9J43Qhw0ekIfhYiG J+bg== X-Gm-Message-State: AOJu0YzKX2/q6VOpgQrXWc4wH+dg6H9t8yNK21iVuFl3NasMkyjJWoWY MKYNlx37wG9uC1ecCkZE1i+jGkjWKtuXaa6lTxuTnZfnwLd+fAmVG5ampTb8tx8dheouWnNcoPY Urux2UxEDEvF5m0kg3hsK2NfzURnKyOzI X-Google-Smtp-Source: AGHT+IHbN1LSSu7QxQ/AmLeNBqshEGEw54HdU9433Q/Wz2kU9PGIGn0oh0Obk08Q+lA+liBPBEsJa8yxKhTDW8pPFwY= X-Received: by 2002:a17:90a:1506:b0:2a2:7817:f591 with SMTP id l6-20020a17090a150600b002a27817f591mr3947304pja.48.1712270230090; Thu, 04 Apr 2024 15:37:10 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 References: <4F094EDA-5058-407D-AF39-06FD934FDE1F@sakiot.com> <68CF373E-6ABF-4471-8992-813B4BA1B508@sakiot.com> <904197f4-afb5-401e-9e17-7a655c5449d0@alec.pl> <655FEA80-9AB4-4AAD-A310-70ED968C97A2@sakiot.com> <8FB87901-02D7-4934-9119-55B21CEDDA9D@sakiot.com> <65e0fc8b-ed4e-4bf8-af2d-77dc7b4bb934@redmagic.org.uk> In-Reply-To: <65e0fc8b-ed4e-4bf8-af2d-77dc7b4bb934@redmagic.org.uk> Date: Thu, 4 Apr 2024 15:36:54 -0700 Message-ID: Subject: Re: [PHP-DEV] [RFC] [Discussion] Support object type in BCMath To: Barney Laurance Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary="000000000000d2ff9c06154cfaee" From: jordan.ledoux@gmail.com (Jordan LeDoux) --000000000000d2ff9c06154cfaee Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, Apr 4, 2024 at 2:19=E2=80=AFPM Barney Laurance wrote: > > I don't think it will be possible to make a good Money class as a child o= f > this. BcNum is a read-only class, so if the constructor of BcNum is final > then the child class won't be able to take the currency as a constructor > param, and won't be able to protect the invariant that currency must be > initialized. If the constructor of BcNum were made non-final then BcNum > wouldn't be able to protect the invariant of the numeric value always bei= ng > initialized once the constructor has returned. And it should be > straightforward to make a good money class as a composition of BcNum and = a > currency enum or string. > > The RFC does not list the constructor as final, and I would not expect it to. > There's probably not any good use case for a subclass to add properties, > unless perhaps the subclass developers are willing do do away with some o= f > the checks that would normally be done on a value object by the PHP runti= me > to keep it valid (maybe replacing them with static analysis checks). But > there are lots of reasonable use cases for subclasses to add methods, eve= n > if they're effectively syntax sugar for static methods with a BcNum typed > param. > > I literally just provided in the quote you are replying to a good use cas= e for a subclass. You can do the same thing with composition yeah, and that might even be better to a lot of people, but I don't think this RFC should take a position AGAINST subclasses and in favor of composition. > Having just written that I've now checked the behavior of DateTime - see > https://3v4l.org/5DQZg . The constructor of that isn't final, so a child > class can replace it with an empty constructor. But if it does that and > leaves the value uninitialized then it blows up on a call to `format`. > That's probably not something we should emulate. DateTimeImmutable behave= s > the same way. > Why not? Writing something like that obviously does not work, and the error would be immediately apparent. Is it possible to create something that will error? Of course. That's not an error that needs to be aggressively guarded against, because the feedback is rapid and obvious. Jordan --000000000000d2ff9c06154cfaee Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


=
On Thu, Apr 4, 2024 at 2:19=E2=80=AFP= M Barney Laurance <barney@redm= agic.org.uk> wrote:

I don't think it will be possible to make a good= Money class as a child of this. BcNum is a read-only class, so if the constructor of BcNum is final then the child class won't be able to take the currency as a constructor param, and won't be able to protect the invariant that currency must be initialized. If the constructor of BcNum were made non-final then BcNum wouldn't be able to protect the invariant of the numeric value always being initialized once the constructor has returned. And it should be straightforward to make a good money class as a composition of BcNum and a currency enum or string.

The RFC does not list the constructor= as final, and I would not expect it to.

There's probably not any good use case for a subclass to add properties, unless perhaps the subclass developers are willing do do away with some of the checks that would normally be done on a value object by the PHP runtime to keep it valid (maybe replacing them with static analysis checks). But there are lots of reasonable use cases for subclasses to add methods, even if they're effectively syntax sugar for static methods with a BcNum typed param.

I literally just provided in the quot= e you are replying to a good use case for a subclass. You can do the same t= hing with composition yeah, and that might even be better to a lot of peopl= e, but I don't think this RFC should take a position AGAINST subclasses= and in favor of composition.

Having just written that I've now checked the behavior of DateTim= e - see https://3v= 4l.org/5DQZg . The constructor of that isn't final, so a child class can replace it with an empty constructor. But if it does that and leaves the value uninitialized then it blows up on a call to `format`. That's probably not something we should emulate. DateTimeImmutable behaves the same way.

=
Why not? Writing something like that obviously does not w= ork, and the error would be immediately apparent. Is it possible to create = something that will error? Of course. That's not an error that needs to= be aggressively guarded against, because the feedback is rapid and obvious= .

Jordan
--000000000000d2ff9c06154cfaee--