Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112135 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 13464 invoked from network); 28 Oct 2020 17:39:55 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 28 Oct 2020 17:39:55 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 1184F180511 for ; Wed, 28 Oct 2020 09:58:53 -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,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-ed1-f45.google.com (mail-ed1-f45.google.com [209.85.208.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 ; Wed, 28 Oct 2020 09:58:52 -0700 (PDT) Received: by mail-ed1-f45.google.com with SMTP id t20so141687edr.11 for ; Wed, 28 Oct 2020 09:58:52 -0700 (PDT) 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=fcgLhV7ouUxkhjAPUiBP0sgPmxxpeqEKauCiIqb3txE=; b=pFB+2KhKewint93QMg3QdIQM6qtXDTFC/I3BcKmsSH+Wyv1fu9Fo4IR1pNXxs1WrAs RCgsjCF4nJI3DkmqSY+E6b2ZsbFOEZg+OGAOpoEpKL7PQ12xRoYb7NB4khtDq18/I3XG 9ld7AietwGimwAfbMmOiVMK/kDfAF7zVA4JEzB0bQN7tur7IHfLoyH3mYshmnFaI7yx1 NnnyEZ7q6m2gOvhcOXsQesKmVRPyccBecpF5+be+2AcNh59i+lzT2cAQvx7dwq+bz+lY 8TFkSNjB1gSCXP5MxgNQs0bA0cWrvmx283/OVYzBB8l1r1p/HTlpCY6N1tYoZ9+95P27 snnw== 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=fcgLhV7ouUxkhjAPUiBP0sgPmxxpeqEKauCiIqb3txE=; b=ogu4Wb8vlGbWD8Hlxb4zh+kuuyrEhB6B4As7SPTW5dQzfwfVmbkjClbd77N8xek1Pi l3eDlqnYcklshfiAu8xTGNQYet8aDZ9GDNsf450OY0VjKyxJhpg6XLp0KFPVzmq00HA6 pocIhGJXc+BZ1Vih23Sd/HebkKye7EGwk/skU7vJ6C/0Tr2ym2EeRp1FAquzJqCLLeIB OWFWMVTbG6T9mWyhY1GT1HR6JzlVeehkDFjsPikVddlaKC6NWlHthmAHPlJsujutMxTE Ik3af4EDq9hE6jKAQI3s53AGmCpUVrMTKzz0vp8TneMLy0EztQFeMLZTZG3+7HxR4SZY NTQQ== X-Gm-Message-State: AOAM530X1bLc6vCx7snDeZFWU+L2rabmAjw4apvQdAltemVs73kighzh Pl22rv7MozXFcEsLV7PDJ2H2wCDWEl1I/JXAyTs= X-Google-Smtp-Source: ABdhPJwAqRPLU/WU5Ti/Gcn7mnKJ8CX79vT8cVsiPvh0klVGO/NhfOkXinNoaH45saCm5jmVtQzIan42XZbVYXuTxKQ= X-Received: by 2002:aa7:c58f:: with SMTP id g15mr8604461edq.99.1603904331274; Wed, 28 Oct 2020 09:58:51 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 28 Oct 2020 17:58:40 +0100 Message-ID: To: Theodore Brown Cc: Rowan Tommins , PHP Internals List Content-Type: multipart/alternative; boundary="000000000000eb5ea705b2be1331" Subject: Re: [PHP-DEV] List of attributes From: nicolas.grekas+php@gmail.com (Nicolas Grekas) --000000000000eb5ea705b2be1331 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi all! about why we'd need nested attributes, here is a discussion about nested validation constraints: https://github.com/symfony/symfony/issues/38503 You'll see that we're looking hard into ways around using nested attributes, but they all fall short. We actually concluded that we should not create a hacky syntax before php-internals settled on the topic. I invite you to review the ideas and arguments posted there to see a real world need on the topic (please don't mind sharing your thoughts about the topic in the PR of course if you think you can help us move forward.) See more comments below: Le ven. 23 oct. 2020 =C3=A0 17:52, Theodore Brown = a =C3=A9crit : > On Tue, Oct 20, 2020 at 10:13 AM Rowan Tommins > wrote: > > > On Mon, 19 Oct 2020 at 16:17, Theodore Brown > wrote: > > > > > > > > In theory nested attributes could be supported in the same way with > > > the `#[]` syntax, but it's more verbose and I think less intuitive > > > (e.g. people may try to use the grouped syntax in this context, but > > > it wouldn't work). Also the combination of brackets delineating both > > > arrays and attributes reduces readability: > > > > > > #[Assert\All([ > > > #[Assert\Email], > > > #[Assert\NotBlank], > > > #[Assert\Length(max: 100)] > > > ])] > > > > > > > I think you're presupposing how a feature would work that hasn't > > even been specced out yet. On the face of it, it would seem logical > > and achievable for the above to be equivalent to this: > > > > #[Assert\All( > > #[ > > Assert\Email, > > Assert\NotBlank, > > Assert\Length(max: 100) > > ] > > )] > > > > i.e. for a list of grouped attributes in nested context to be > > equivalent to an array of nested attributes. > > Hi Rowan, > > The problem with this is that it results in inconsistent semantics, > where the number of items in an attribute group results in a > different type of value being passed. I.e. if you remove two of the > three attributes from the list, suddenly the code will break since > the `Assert\All` attribute is no longer being passed an array. > Yes. This would be solved by NOT accepting #[] inside attribute declarations. Since we borrowed from Rust, let's borrow this again: http://web.mit.edu/rust-lang_v1.25/arch/amd64_ubuntu1404/share/doc/rust/htm= l/reference/attributes.html#attributes The Rust syntax accepts the #[] only on the outside of the declaration. The example above should be written as: #[Assert\All([ Assert\Email(), Assert\NotBlank(), Assert\Length(max: 100) ])] The closing brackets () would be required to remove any ambiguity with constants. Since constant expressions won't ever allow functions in them, this isn't ambiguous with function calls. > // error when Assert\All is instantiated since it's no longer being > // passed an array of attributes: > > #[Assert\All( > #[ > Assert\Email, > ] > )] > > So when removing nested attributes from a list such that there's only > one left in the list, you'd have to remember to additionally wrap the > attribute group in array brackets: > > #[Assert\All( > [#[ > Assert\Email, > ]] > )] > > And of course when you want to add additional attributes to a group > that originally had only one attribute, you'd have to remember to > remove the extra array brackets. > > Generally speaking, having the number of items in a list change the > type of the list is a recipe for confusion and unexpected errors. So > I think the only sane approach is to ban using the grouped syntax in > combination with nested attributes. > > Unfortunately, no one thought through these issues when proposing to > change the shorter attribute syntax away from @@ and add the grouped > syntax, and now it seems like we're stuck with a syntax that is > unnecessarily complex and confusing to use for nested attributes. > > > Unless nested attributes were limited to being direct arguments to > > another attribute, the *semantics* of nested attributes inside > > arrays would have to be defined anyway (e.g. how they would look in > > reflection, whether they would be recursively instantiated by > > newInstance(), etc). > > Yes, the exact semantics of nested attributes need to be defined, but > this is a completely separate topic from the grouped syntax issue > described above. > > As a user I would expect nested attributes to be reflected the same > as any other attribute (i.e. as a `ReflectionAttribute` instance). > Calling `getArguments` on a parent attribute would return an array > with `ReflectionAttribute` objects for any nested attribute passed > as a direct argument or array value. > I 100% agree with this. On first thought I think it makes sense to recursively instantiate > nested attributes when calling `newInstance` on a parent attribute. > This would allow attribute class constructors to specify the type > for each attribute argument. But again, this is a separate discussion > from the issue of nested attribute syntax. > Also 100% agree with this. Cheers, Nicolas --000000000000eb5ea705b2be1331--