Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:118118 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 43474 invoked from network); 29 Jun 2022 06:49:11 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 29 Jun 2022 06:49:11 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 96300180211 for ; Wed, 29 Jun 2022 01:40:23 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-yb1-f172.google.com (mail-yb1-f172.google.com [209.85.219.172]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 29 Jun 2022 01:40:23 -0700 (PDT) Received: by mail-yb1-f172.google.com with SMTP id o19so20124449ybg.2 for ; Wed, 29 Jun 2022 01:40:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=knd5hhXTmaX6i0DL/lgcWkMvikutY1/T+Q4nuHYzNB0=; b=N/MZ/layGE+mhWhVBWEXQ2eMFnKC9wgLSWnBllIo/OucXP15bHN2hIe9diSrsYvkuY d7JdYWOGbtWXTHgCNhLIZ+FPUnvNyoTpr9IxJw0ylKwbtDN2sz9O2OcrSnJZvBXh3pVV vKl1FGr2QjeIAVOqmyyL0E//fsFzwcYeRF6rF6FlJBj2/rcsWGIKDj9YcKaRY4Y22Ko1 DG46/7BYGupYvI4CBxACYm5xHcO3PMcXecbx1sI0QbsjfjqlnN25RIIxQ1m6iHqQ/lUl x1KJGql65edyNXHLPwtWRCQ+4iUEx9PM1FivIwQPdPJc6/xooe5bP0Fjy9hHkbzBOkOw MZ8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=knd5hhXTmaX6i0DL/lgcWkMvikutY1/T+Q4nuHYzNB0=; b=HtwSqO9CJNTkeqZByNTRARJVFTW3+OU3J1k84dXXbDcoSD1hJMRt8eY/vcbfCvnGxd eggMUXzEEVZFIOm2J3w8HBAqx9oLMJHT4lII1y5EghYwGN1Edrw90SVFxKe5hvV1dVN/ hJvLYRIh8a3nSbFvXxkKmjpv6nQvyQMeM7O/OFhMwIQDFReEsAfs9NwwYubdvH6/TqM1 QNQAIuAmEpQ/8WzsENGZXGtRTKpaW0T7APkFJvuQwa8IruuB02+dFCMD3UilHuxPJ4er byPBM/+fCb3f6aAJ1iTYTEbgzOIAjxDwct3MBUuYsTD+sZwlRxKNlXq5iCCoyU4snTOa kQsw== X-Gm-Message-State: AJIora9zljBql1kAVYrq8oMYXrbpeuWsWmDETdI7azDZ2twr3nfoMWF1 fkRjGspjnylRSooMbl1ltUg+am7p4zyrXi+jnQ== X-Google-Smtp-Source: AGRyM1tPLXFgVdUDNXoJtT0sMf1TzHeVOt4tB85S1eCFJagRNLxlKYvhQpM+kAJvUBqxFaMNzRTmKWtY31Y5PBPc5u4= X-Received: by 2002:a25:bdc9:0:b0:668:3d05:e009 with SMTP id g9-20020a25bdc9000000b006683d05e009mr2048663ybk.402.1656492022569; Wed, 29 Jun 2022 01:40:22 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 29 Jun 2022 10:40:11 +0200 Message-ID: To: Go Kudo Cc: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] [RFC] [Under Discussion] Random Extension Improvement From: guilliam.xavier@gmail.com (Guilliam Xavier) >> > https://wiki.php.net/rfc/random_extension_improvement >> >> I just realized a little thing: in the array_rand() example, for >> $beforeSingle, it would probably be "more realistic" to omit `, 1` >> (which is already the default for $num). >> >> Note: for `Randomizer::pickArrayKeys(array $array, int $num): array`, >> it makes sense that $num does *not* have a default value (1 would be >> "weird" because the method always returns a *list of keys*, and >> count($array) [via null] would be "useless" because keys are returned >> *in their original order* [so it would make the method equivalent to >> array_keys($array) by default]), >> and that's probably a good thing (it forces to update the call by >> adding an explicit `, 1` argument and reminds to add a `[0]` or >> similar on the returned value). >> >> An alternative design would be `Randomizer::pickArrayKey(array >> $array): int|string`, but migrating existing uses with $num != 1 would >> be harder, so probably not better. > > This is certainly a complicated issue. > > I proposed the signature `Randomizer::arrayPickKeys(array $array, int $num): array` because it can be solved with the current PHP sugar syntax and the default value of $num is 1 despite the name "arrayPickKeys". > > However, this is a bit tricky and may not be user-friendly for the average user. > > So, how about adding two methods, `Randomizer::arrayPickKey(array $array): int|string` and `Randomizer::arrayPickKeys(array $array, int $num): array`? > > This may seem redundant, but it may avoid user confusion. Sorry if I wasn't clear: I just suggested to make this little change in the example: ```diff -$beforeSingle = array_rand(['foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'], 1); // (string) foo +$beforeSingle = array_rand(['foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz']); // (string) foo ``` to make it more "realistic". As concerns the rest (about pickArrayKeys): sorry for the digression, I was just "thinking out loud", I *don't* want any change there (first, it makes sense that pickArrayKeys has `int $num` *without* a default value [even if array_rand has 1]; second, "pickArrayKey" [if really wanted] is trivial to implement in userland as a wrapper around pickArrayKeys [but the opposite would not be so], and I don't think that adding *both* methods to Randomizer is desirable either [better keep it simple/minimal]). Regards, -- Guilliam Xavier