Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122960 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 445671A009C for ; Fri, 5 Apr 2024 09:49:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1712310573; bh=uL2IHjwshM42XzhHGDA2Oz3VJeDw9if2jBlFncacZzg=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=cq2P5RhKg0LcYjXzE015ejxEDHayV3c/rRvrr7gX/EWWEWIwdmP7zrjg2gW9sIaO1 ZfG6OQqUVNglBLTtWL0sgXnlKA6W+SGGLAW51fHWx1z1WDlnAabr5RTa2cn74Xpjl5 OFZaF1UoUR9a0ruzu5bqEATyykMhUFFsRllEvw5ETOLHralOb3QmiCmx2aqx6eodJh GrMCUZhQ8gGIRuu/DM9edgqwqEcOurzZiQ2A+mUpMoiDLcIWPWdjEW6HQ6+PQGPudZ LCJeobDOUA1iBP09cdS2BkYifPe1iCgprNM6fZS6uFnB4r7TW6lXC/d39pBG5RGyq7 SkU5jy6WnNufQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id D98BF180688 for ; Fri, 5 Apr 2024 09:49:30 +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,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 chrono.xqk7.com (chrono.xqk7.com [176.9.45.72]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 5 Apr 2024 09:49:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bastelstu.be; s=mail20171119; t=1712310538; bh=UZBDojpu/cw1fo1mR+0nM6S/uHg/olhjj+0oEbOz4pI=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type:from:to:cc:subject:message-id; b=ZXaPYhkSQGXT7RcJVbb8lgaA6zfYqdoMNqIMrVQ13WPTk+RgE+3KU6YA3YZ2zkK8a KBeTrtv31aDFmXjpi96uWvdtL+g2QJPVD/5q7//TvfkDWFiCUP1W1WLyBRjwwblOVm diuXyLIEFDfXzxsRY52XV20TTHbaOqgAINzdn1GEOeiufbroNiQdOuHHdUNhbR+0ER qe6MaqghQrWyFikz5x4gFIK7Q7/aigOHzXDjmd675a0sYth3dXgXFGqdw/fH9dv23D PQzSdlXIfE8FqXtKV2FND1GhSC5j++fCq5rbKheOXT6TqwhJg/t4ogyPixs63PdFqQ bzMOxt+kmDnOw== Message-ID: <8035a2b2-f8dc-4a25-98eb-1d19c986bc75@bastelstu.be> Date: Fri, 5 Apr 2024 11:48:57 +0200 Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 Subject: Re: [PHP-DEV] [RFC] [Discussion] Support object type in BCMath To: Jordan LeDoux , Saki Takamachi Cc: Lynn , Aleksander Machniak , php internals 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> Content-Language: en-US In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit From: tim@bastelstu.be (=?UTF-8?Q?Tim_D=C3=BCsterhus?=) Hi On 4/4/24 02:54, Jordan LeDoux wrote: >> If make a class final, users will not be able to add arbitrary methods, so >> I think making each method final. Although it is possible to completely >> separate behavior between method and opcode calculations, this is >> inconsistent and confusing to users and should be avoided. >> > > > Right, if a class is not final but has only final methods (including >> constructor) and no protected properties then it allows people to extend it >> but not break any encapsulation or (I think) impose any more BC >> requirements on the maintainer (unless we care about conflicting with >> method names of child classes when updating). It lets people just do >> something a bit like adding 'extension methods' in C#. I like it. People >> could write e.g. `$bcChildNumber->isPrime()` instead of >> `BcNumberUtils::isPrime($bcNumber)` > > > Yeah, I suspect the most common child class will be something like "Money" > that additionally has a parameter denoting what currency the value is in, > since that is by far the most common use case for arbitrary precision math > in PHP. Making the calculation methods final should do fine in my opinion. Your `Money` example would allow for unsound and/or non-sense behavior, such as: $fiveEuros = new Money(5, 'EUR'); $tenDollars = new Money(10, 'EUR'); $what = $fiveEuros + $tenDollars; What would you expect to be in $what? A `BcMath\Number(15)`? ---------------- The BcMath\Number class *should* absolutely be final, as a number is a number is a number. Allowing extension just to be able to write $number->isPrime() instead of isPrime($number) will allow for very confusing code, such as the example above, with no way to work around it in userland. It also makes interoperability between two different libraries that expect their own extensions to work very painful. Consider the following example: class PrimalityTestingNumber extends Number { public function isPrime(): bool { } } class ParityTestingNumber extends Number { public function isEven(): bool { } public function isOdd(): bool { } } If I now want to create a function to check whether a number is an even prime, I need to do something like this: function isEvenPrime(Number $n) { return (new PrimalityTestingNumber($n))->isPrime() && (new ParityTestingNumber($n))->isEven(); } This use case would be much better solved in a generic way using something like this "Extension Methods" proposal: https://externals.io/message/118395#118395 Best regards Tim Düsterhus