Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120269 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 47124 invoked from network); 14 May 2023 10:59:26 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 14 May 2023 10:59:26 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 06D541804C6 for ; Sun, 14 May 2023 03:59:25 -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_H3,RCVD_IN_MSPIKE_WL,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-oa1-f54.google.com (mail-oa1-f54.google.com [209.85.160.54]) (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 ; Sun, 14 May 2023 03:59:24 -0700 (PDT) Received: by mail-oa1-f54.google.com with SMTP id 586e51a60fabf-192814aa343so9229920fac.1 for ; Sun, 14 May 2023 03:59:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684061963; x=1686653963; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=T5Xd4Yv7DNtM7KI4vsxcO0xwH6T7PxwXPvPnRiuoBkE=; b=fjuKTA2P0ZPbm6xXQ4lGLKKMb9Fdcw27eG6O6j8B3P7ukP1Es4DAAI4A8eznmk5dfk MQcLeq7S++oJyqg9o1FGxGSDyRs/I5bDPSlPHUpXuG4NehJ4ine568WGCRYsm6/bLSqS iR8e9308LIxjlrMzPzbqJx5B1bQrjkjyQsRaiOyIvVpg4mWXUhhKOpU45TrnGpxuysWa knZxy99+/GerSenzlNaZj2YZrQBfqS26/wVT+9kSVCUkjVz0TIBvGPMFC0Y2JQQo0ab8 2DVb1cSPL2S3nXNxaidduZmakTJxTkCVIfn6qKB9eY5eiCpShrU3ZO+tE2VREgQoyfXk TV4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684061963; x=1686653963; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=T5Xd4Yv7DNtM7KI4vsxcO0xwH6T7PxwXPvPnRiuoBkE=; b=FkdjhT72YKVcahgFVU5jROZJg8Toybb1ImhFQTGolRI35WSAf5xU1p2bTdO5u0df5S G+g2EZVy0zUlabUWd9P5PlPaTa0/StHAc56IoO4Wlbc8aXwi4tFXyG2fvQhdstLkmaQE OGskGcCODykJ0dD0cfAtVPhUxb5RlUFAwT3pkub/CkqH9/PJv1ixNcuA5crbesy/dK+7 8Lf8Us7HiFUqFjku0OHe171/pDUh3vTwHu5sbn/1BzUZIZ1NpNcMwxmWg4+54+5XYkC1 qaENreXKuGM37XJz+nMbThifz8rPqsY3GZdh/088dQdLbrHlLgvGdafMIhGihAgp4vN9 PvMQ== X-Gm-Message-State: AC+VfDxGy146wG6UCiVs8aCB8R0MCIWINcz9whv78PtvKs204bi0k/Lc OsP1IQSpPZbTLCzMdoyBlP9gVO0XDM6SgiiqrS0Y699OwlbRwQ== X-Google-Smtp-Source: ACHHUZ51/WHOpXcqVA5aGl08/kbz+wuev7w44mWhrN4FGE9Zk57EQdRE07ayrMOczq5Kt0rXqp136EIvIIZZJSnGsPo= X-Received: by 2002:a05:6870:e604:b0:172:2d00:99f7 with SMTP id q4-20020a056870e60400b001722d0099f7mr11892400oag.20.1684061963594; Sun, 14 May 2023 03:59:23 -0700 (PDT) MIME-Version: 1.0 References: <436378BB-FDFA-43BD-A633-C030C347E683@gmail.com> In-Reply-To: Date: Sun, 14 May 2023 12:59:13 +0200 Message-ID: To: Rowan Tommins Cc: internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] [RFC] [Discussion] nameof From: landers.robert@gmail.com (Robert Landers) On Sun, May 14, 2023 at 11:16=E2=80=AFAM Rowan Tommins wrote: > > On 13 May 2023 16:40:27 BST, Robert Landers wr= ote: > > Most of the time, you want the unqualified name > > ... > >This is especially important when using traits and aliases: > > I don't think trait renaming is the same thing as aliasing a qualified na= me. In a sense, it's actually the opposite: with a trait, the compiler is i= nserting the code of the method under the new name, and the name from the t= rait can no longer be used to access it; for namespace imports, the compile= r is substituting the fully-qualified name, and the *local alias* can no lo= nger be used to access it. As someone who has had limited interaction with the engine code itself but has been using PHP, daily, for nearly 15 years, I'm not sure this technical distinction makes sense in the context of programming in PHP. When using traits, I don't think of it as 'inserting code', but rather I think of it as 'using code' -- you do use `use TraitName` after all and it is very similar to the top-level using statement. > >In this case, if you want to use nameof(TEST) in an error message or > >something else, it would be surprising to get \A\A instead of TEST. > > Would it? As I said before, my intuition was completely the opposite, tha= t one of the advantages of writing nameof(TEST) rather than just 'TEST' is = that it would resolve namespace imports the same way ::class does. > > Can you give an example of such an error message that would want to expos= e the local alias? Which error message helps in resolving the error and would be written by someone writing the code? use function \Path\To\Function\Serializer\to_json as json_serializer; // a bunch of code using json_serializer() throw new Exception('json_serializer returned invalid json'); // or throw new Exception('\Path\To\Function\Serializer\to_json returned invalid json'); The former appears more natural and helps to trace the issue. Otherwise, you end up in the file, trying to dig through usings to figure out where usages of \Path\To\Function\Serializer\to_json are in the file because, at first glance, it appears to not be there. This would also be weird in the case of constants/functions in the global space, because fully qualified names begin with a "\" which feels unnatural when reading things meant for humans (such as error messages). > >So, for your example, we can instead call nameof(\Acme\bar(...)) > >instead of the aliased name when passing to another context: > > > >use function Acme\bar as foo; > >... > >#[SomeAttribute(callback: nameof(\Acme\bar(...))] > >... > > > >I hope this helps! > > > Not really, I'm afraid. If I have to write out the fully-qualified name, = why would I bother with nameof() rather than just using a string? If you write it in a string you really will, actually, have to write out the fully qualified name because an IDE won't know you're referencing a language construct and be able to autocomplete it for you. If you rename the function or remove it, you won't be able to use static analysis to locate these strings, you'll have to manually search-and-replace the codebase, which opens up space for human error. nameof() gives you a fighting chance of actually finding ALL the usages in one go. Personally, I've found that refactoring the names of things in PHP is one of the most dangerous coding activities one can do in PHP. They may be in a string, a database, on the wire via `serialize(),` or any number of places. Making this a safer activity is one of my personal goals. > The more I read in this thread, the less I understand what the point of n= ameof() actually is, and how it would be used. IMHO, this comment isn't constructive or helpful, how can I help you understand? I'm more than happy to answer any and all questions, but comments like this are pretty demoralizing when there are only a few people discussing the feature in a community I'm relatively new to -- are my answers not clear enough, would a call help, or should I reference other language's implementations and rationales? I don't know the unwritten rules here, and being helpful and constructive can move the conversation forward instead of reaching impasses. Personally, I've been in war and dealt with far worse than this, but someone who hasn't been through what I've been through may not be as "thick-skinned" and just walk away. Just something to keep in mind for future interactions.