Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112920 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 97251 invoked from network); 18 Jan 2021 14:54:45 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 18 Jan 2021 14:54:45 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 61F031804E4 for ; Mon, 18 Jan 2021 06:34:14 -0800 (PST) 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,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-lf1-f45.google.com (mail-lf1-f45.google.com [209.85.167.45]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 18 Jan 2021 06:34:13 -0800 (PST) Received: by mail-lf1-f45.google.com with SMTP id v67so24384162lfa.0 for ; Mon, 18 Jan 2021 06:34:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=9pntn2g9qBsAjhI+FRTTVb5WmZQmG5+Sprzbjaru5+8=; b=UKEG5uQsG8woADCXplDn0XmtaqsuXsPXBv423AmCOW/xO40AVrPHudDk4LjpLvOlK8 A8A7LjhTUtkKdRuUl63zVFx4DlH7R1A2rG3j31IUL4SraFxoNUxqRYc50bUSTZ1pKSDs HiXngucEnbE9XFzwoS1VqnRz5DsZg7yJgg5BKXmb9n6pmGvhHu3voOiE2gSe3MZE7SKR SgT8h28UIAiMtr/Bn+EnE1C/Q91K9ekB841Ro9MYmp5qKXbBggzC2v1LXnDR0IYjR/2V y50VGs0cooRU7AF2qyBgLv2tUSREA5jMpaLH73kpiK2q7aw0BnUruKntAoX18BfdRn6B +U2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=9pntn2g9qBsAjhI+FRTTVb5WmZQmG5+Sprzbjaru5+8=; b=M0d8PdIWZVHpFAULPBlBG1cg7384SJ42xycB61B8uy6yQ7sMCkU1mxiZtWgyZBjND4 FtUqaFFveDbqQXMRnrJfsIuocA8Uel74ELxqYE/q18Ysgq+baBu/Z4VN3juHKOwxuT9Z TZCcWukO7i3Wi/Eo2ChjDUp2Y+bEiZZ4aCeeSFLaE6qGtOzu2BiBGZhMnXOq83ugrOBA yBYO+/Mqpr68CQd0wVJKx+J4PJTkh8rpmZGJOw8lQtfuDR3aKc8fCwGFrmbwuVLQM7qs PjRsaWPcJ0RR+R352A19A+xoJo3ndvb3/R2sxl6MGdDCDxMOXpp82terZDtvAfCYKXRT 2slg== X-Gm-Message-State: AOAM531fif4wcd8H76G7YeKGE1xeDUGn62/hJc2TdFlAS1932nnMTDSF qq4VOTDtXcJ5egTMKiHx5dybyGBZdIMEghQSITE= X-Google-Smtp-Source: ABdhPJyUMYaIZLL7yEU+1yVYXXasoNTmWMQWRI7+t+fLGH4Qrn7xbiVJylxAPQM2rU9EjF9tsGwLZ65gQ/+DAaQ+6Ag= X-Received: by 2002:a19:4c44:: with SMTP id z65mr10817469lfa.315.1610980451359; Mon, 18 Jan 2021 06:34:11 -0800 (PST) MIME-Version: 1.0 References: <39ccd4f9-cb63-549f-d34e-0c5e473598a2@gmail.com> In-Reply-To: <39ccd4f9-cb63-549f-d34e-0c5e473598a2@gmail.com> Date: Mon, 18 Jan 2021 15:33:55 +0100 Message-ID: To: Rowan Tommins Cc: PHP internals Content-Type: multipart/alternative; boundary="0000000000008b1f3505b92d9d2e" Subject: Re: [PHP-DEV] [RFC] Allow object keys in arrays From: nikita.ppv@gmail.com (Nikita Popov) --0000000000008b1f3505b92d9d2e Content-Type: text/plain; charset="UTF-8" On Wed, Jan 13, 2021 at 10:08 AM Rowan Tommins wrote: > On 12/01/2021 16:51, Marco Pivetta wrote: > > Whether the problem can be mitigated is what should be discussed, but the > > problem is objectively there. > > > Hi all, > > Like others, I like the *idea* of object keys, but worry about its > practical impact. > > A few scatter-gun thoughts on possible approaches, I don't think any of > them is the solution, but maybe they'll spark someone else to further > ideas: > > > - Only allow objects that are "stringable" (i.e. implement __toString), > but don't actually call it. This retains the safety of code using > "(string)$key", but not code passing to a constraint of "string|int". > (It also means enums will all have to have an __toString, which might be > a price worth paying.) > > - Create a new type, which is like an array but allows object keys, with > its own literal syntax. e.g. > > $hash = hash['foo' => 42, $someObject => 69]; > assert(is_array($hash) === false); > assert(is_iterable($hash) === true); > > - Invent a syntax for initialising a custom collection object from a > literal with arbitrary keys and values, but not actually initialise it > as an array. Even without generics, this would make it much more > attractive to replace more arrays with specific collection types. > I don't think a separate dictionary type would really help (in the context of this discussion). If you're working with arrays, you already either treat it as a vector (in which case the keys are irrelevant) or as a dictionary (in which case you would need to deal with object keys regardless of whether you call it "array" or "dict"). I think a dedicated dictionary type can have other benefits (primarily removing numeric string -> int canonicalization), but I don't think that distinguishing allowed key types based on it is a good idea. (If we introduced such a type, I would expect it to have the same key requirements as normal arrays, because both types would have to be strongly interoperable, and allow mostly seamless conversions.) Regards, Nikita --0000000000008b1f3505b92d9d2e--