Re: [RFC] [Discussion] Add bcdivmod to BCMath

From: Date: Wed, 10 Jul 2024 08:55:01 +0000
Subject: Re: [RFC] [Discussion] Add bcdivmod to BCMath
References: 1 2 3 4  Groups: php.internals 
Request: Send a blank email to internals+get-124334@lists.php.net to get a copy of this message
On Mon, 1 Jul 2024, Saki Takamachi wrote: > Hi, > > >> Just a suggestion: what about making the returned array an associative > >> array ? Like so: > >> ``` > >> array( > >> 'quotient' => 61, > >> 'remainder' => 1, > >> ); > >> ``` > >> This would remove the need for devs to remember the order of the return > >> values and would make the return value self-documenting. > > > > An associative array would combine the worst of an array (no IDE autocompletion, no strong > > typing, increased memory usage) with the worst of an object (no easy way to extract the values into > > local variables with array destructuring). > > > > The example in the RFC doesn't show it, but the following makes the proposed API > > really convenient to use: > > > > $slicesOfPizza = new BcMath\Number(8); > > $mouthsToFeed = new BcMath\Number(3); > > [$perMouth, $slicesLeft] = $slicesOfPizza->divmod($mouthsToFeed); > > > > Note how the order of values matches the words in the method name. First the result of > > 'div', then the result of 'mod’. > > > Thanks, I have added this example to the RFC (Please let me know if you have any problems). The example has: [$perMouth, $slicesLeft] = $slicesOfPizza->divmod($mouthsToFeed); // $perMouth->value is '2' // $slicesLeft->value is '2' Shouldn't that be (without the ->value) ? [$perMouth, $slicesLeft] = $slicesOfPizza->divmod($mouthsToFeed); // $perMouth is '2' // $slicesLeft is '2' cheers, Derick

« previous php.internals (#124334) next »