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

From: Date: Sun, 30 Jun 2024 14:25:16 +0000
Subject: Re: [RFC] [Discussion] Add bcdivmod to BCMath
References: 1 2  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
Hi

On 6/30/24 16:16, Juliette Reinders Folmer wrote:
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'. Best regards Tim Düsterhus

Thread (14 messages)

« previous php.internals (#124098) next »