Re: [PHP-DEV] [RFC] [Under Discussion] Random Extension Improvement

  118004
June 20, 2022 05:20 g-kudo@colopl.co.jp (Go Kudo)
2022年6月19日(日) 16:25 Marc <marc@mabe.berlin>:

> Hi Go Kudo, > > About "pickArrayKey" I don't like the behavior change on number of array > keys to return either a single key or an array of keys. > It would be better to have different methods for both cases. > > Also it does not describe the behavior if you are requesting more elements > as in the given array. > > Will the returned list of keys be unique? > > Additionally in array_rand documentation: > > > If multiple keys are returned, they will be returned in the order they > were present in the original array. > > Does that make sense? And would your method behave the same? > > > Am 17.06.2022 21:41 schrieb Go Kudo <g-kudo@colopl.co.jp>: > > Hi internals. > > An RFC has been created to fix an issue in Random Extension 5.x. > > https://wiki.php.net/rfc/random_extension_improvement > > Voting on this RFC will begin in two weeks (2022-07-02), in time for the > PHP 8.2 Feature Freeze. (Vote finished in 2022-07-16, Feature Freeze is > 2022-07-19) > > In the unlikely event that the Random Extension 5.x RFC is rejected, this > RFC will become invalid regardless of the outcome of the vote. > > Best regards > Go Kudo > > > Hi Marc.
> It would be better to have different methods for both cases.
I agree. The current specification is intended to be a drop-in replacement for `array_rand()`. A better API should be provided. In my opinion, the ability to return a single key by value when `int $num` is 1 is unnecessary. I don't need the ability to return a single key by value if `int $num` is 1, since I can simply refer to the first element of the array even if it is returned as an array. What about a signature like `Randomizer::pickArrayKeys(array $array, int $num): array`? The previous behavior when `int $num` is 1 can be easily reproduced as follows: ```php $randomizer = new Random\Randomizer(new Random\Engine\Mt19937(1234, MT_RAND_PHP)); [$key] = $randomizer->pickArrayKeys(['foo', 'bar', 'baz'], 1); ```php
> If multiple keys are returned, they will be returned in the order they were present in the original array.
Perhaps there is no particular significance to this specification. I don't think it is necessary, but considering the drop-in replacement from array_rand(), I think it is desirable to leave it as is. At least as of PHP 8.2, we would like to avoid BC Break as much as possible.. Best regards Go Kudo
  118023
June 20, 2022 17:00 tim@bastelstu.be (=?UTF-8?Q?Tim_D=c3=bcsterhus?=)
Hi

On 6/20/22 07:20, Go Kudo wrote:
> What about a signature like `Randomizer::pickArrayKeys(array $array, int > $num): array`? > The previous behavior when `int $num` is 1 can be easily reproduced as > follows: > > ```php > $randomizer = new Random\Randomizer(new Random\Engine\Mt19937(1234, > MT_RAND_PHP)); > [$key] = $randomizer->pickArrayKeys(['foo', 'bar', 'baz'], 1); > ```php
I like that one.
>> If multiple keys are returned, they will be returned in the order they > were present in the original array. > > Perhaps there is no particular significance to this specification. I don't > think it is necessary, but considering the drop-in replacement from > array_rand(), I think it is desirable to leave it as is.
I agree. Best regards Tim Düsterhus