Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120306 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 39217 invoked from network); 16 May 2023 01:18:28 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 16 May 2023 01:18:28 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 8EAC21804AA for ; Mon, 15 May 2023 18:18: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=-1.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,MISSING_HEADERS, 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-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) (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 ; Mon, 15 May 2023 18:18:23 -0700 (PDT) Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-3f475364065so13334205e9.1 for ; Mon, 15 May 2023 18:18:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684199901; x=1686791901; h=content-transfer-encoding:cc:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=fy4W2Q2RB92o2WU96wCp0zzaVVkxiY4yqRcc3LR0XX0=; b=e778UhBmwTgeR33WSV2zDqagVw/AvF3RhV38rvSgJUBeP+Chk2qcyeAtJGjEe7Tlqf km4hLAcnuSegPU6+5v5WFjqn8WO9v2UgpRbryG2kDlH8e+YLvjAKQvAi2fudGNOHQk9F nBpa3w+kN2sAtnYQYzamFGo57rWTznx0d9QAXKRnw8uo6ZMNcNXoYtvhHBlkkV+0aCQi AIWCNO+MpnNFF+2f+24g3jEphRxPqt1r4xttrmw4kQM27pd4FC1cBgd8AyT56aSGp+Kj VCMO69zc0OJsFBT1rVDeTbsfrLO44xkuvXDeTCn7ZoGbcC8faj3dHqvI8zgkyDzTL3qo BJTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684199901; x=1686791901; h=content-transfer-encoding:cc: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=fy4W2Q2RB92o2WU96wCp0zzaVVkxiY4yqRcc3LR0XX0=; b=CtSG8Rc/4VKDEQKa4AMjfTyQH0RIzRsFe3b/eSAf58F1jDl3gOYInTcLcCK3tqZEkC Ga52T9EWiJNNTVue4XNpKU0n3LRcr5SZaYRKkrK0jQ2sb1NdQEJ3M9f5EBeWhpz9r11G kTjvTvpSuN5yAqRbw8xwcWpWdzqnb+P6Qg7dACJpoLlB3fwIK1j5xRbFWkO/YlUSO9pH Yhw9q4ZckNGlNc1gHr30DEdB52AZkOLC10PriROPyVBT76OeVnv2Q9vxg4TcZE6L+lP5 Fd/Kt3FROE8I8XOMlMIhFYt1GnmTBTa9fDC8L/GkkbjKRcgihORGVZOfPz+R+uwk7NKt 9LNg== X-Gm-Message-State: AC+VfDxJiOagTEBmQ/ZjBEKy/h/YajclynS3SOATSkcVL37tYsemSQhA 2tFMunx/zsOYO1uh11jXIjfIonQ8M9wH0II9PtQVTS8wrgM= X-Google-Smtp-Source: ACHHUZ6IILcz/PROwONmh4VEl8O8y7z+y+qv8jFH6kiLYDLTxIyWLN8H24Mzug0huY40BK+g5JZZmZz4pFtGQyM5LmE= X-Received: by 2002:adf:dd88:0:b0:307:5561:5eec with SMTP id x8-20020adfdd88000000b0030755615eecmr625230wrl.0.1684199901152; Mon, 15 May 2023 18:18:21 -0700 (PDT) MIME-Version: 1.0 References: <641b1ca0-d33f-4f38-ae64-81b4abce24da@app.fastmail.com> <4947b8d1-1c1d-445d-ba9b-a21f21772548@app.fastmail.com> In-Reply-To: <4947b8d1-1c1d-445d-ba9b-a21f21772548@app.fastmail.com> Date: Tue, 16 May 2023 08:18:08 +0700 Message-ID: Cc: php internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] [RFC] Property hooks, nee accessors From: the.liquid.metal@gmail.com (Hendra Gunawan) > Secondary votes are generally discouraged. I can see the argument for wa= nting a short-short version of set, given how common the validation use cas= e is, but =3D> is almost universally the "evaluates to" symbol, so using th= at for a set operation rather than get just feels weirdly inconsistent. If= there were a set-only shorthand, it should be something else. > > That said, the ideal for validation would probably be guard clauses, like= this, but that would be a completely different RFC for another time. > ``` > function foo(string $name where strlen($name) < 10) { ... } > > class Foo { > public function __construct( > public string $name where strlen($name) < 10; > ) {} > } > ``` ```guard``` seems a subset feature of ```hook```, although you want to expand its use case in parameter. Property and parameter are variable. If ```guard``` can be implemented in both case as you write above, theoretically ```hook``` can be either. I will choose ```hook``` rather than ```guard``` to anticipate future expansion. Isn't it better to choose one broad concept than many? I found a contradiction here. Previously you said that: > ... the problem with omitting the {} is that it creates yet another synta= x variant. That makes it harder for the parser, for static analyzers, for u= ser-space parsing tools like php-parser, etc. That's more work for everyone= for fairly little gain. And here, you introduce a syntax variant. Is it really significant if we introduce a new ```where``` keyword ```compared to the already used ```get``` keyword? I will clarify here in case there is misunderstanding: * If we have more than one hook per property, then the syntax will be the same as proposed by RFC. * If we have only one hook per property, than the syntax are: ``` public function __construct( public string $prop1 get =3D> strtoupper($this->_prop1), ){} public function __construct( public string $prop2 set =3D> $field =3D strtolower($value), ){} public function __construct( public string $prop3 get { $temp =3D strtoupper($this->_prop1); return substr($temp, 0, 10) . '...'; }, ){} public function __construct( public string $prop4 set { $temp =3D strtolower($value); $field =3D substr($temp, 0, 10); }, ){} ``` Note: Please ignore any error. The ending comma is actually part of the parameter list, not a hook. So it becomes optional if the param is only one or it is the last param. All the examples are constructor property promotion, because that is my only concern. Because the space is limited, the more consistent the code, the better. If I have to write hooks outside CPP, I don't mind using the other syntax. There's plenty of room to fill, and consistency is tolerable. Should we postpone the implementation of the shortest version? Hopefully we have enough time to decide which one will be implemented next. > > ... Ilija also noted to me off-list that you could also have a set hook t= hat throws an exception rather than just being null, which would also work = to prevent writes. Such a coincidence. I was going to revise my code: ``` // cache once and freeze, // and without additional hidden property // in case asymmetric visibility in not implemented yet public string $prop1 { get =3D> $field ??=3D $this->heavyTask($this->propx, $prop->y); set =3D> throw new Exception("prop1 is read only"); } ``` > I think an unset hook is a fine addition in the future. The way the code= is implemented it should be straightforward to add. However, it feels lik= e scope creep to include it right now, especially when there are workaround= s available. If there's a clear consensus from voters to include it, thoug= h, we can consider that. (Meaning, anyone that would favor including an un= set hook now, speak up or we'll assume it's better to stick with just get/s= et for now.) +1 for including ```unset``` as part of this RFC.