Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121021 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 98507 invoked from network); 8 Sep 2023 20:27:33 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 8 Sep 2023 20:27:33 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id D8524180083 for ; Fri, 8 Sep 2023 13:27:31 -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,HTML_MESSAGE, 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-yb1-f177.google.com (mail-yb1-f177.google.com [209.85.219.177]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature ECDSA (P-256) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 8 Sep 2023 13:27:28 -0700 (PDT) Received: by mail-yb1-f177.google.com with SMTP id 3f1490d57ef6-d7e6d9665bcso2197866276.1 for ; Fri, 08 Sep 2023 13:27:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1694204847; x=1694809647; darn=lists.php.net; h=cc:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=LV+tuJCp9H9ddUWpTrZna2FDHSHYOfBxnmXpUVNpo1I=; b=hXOrFGSBFN/DSPO5ZG2goABc1PewBy6RnJ4DGyCqWyUqi0AVaNLRsKUHcL6CkJPtXX 9mjTOFLwuF9BOBDWcgriH+4ZYqkSUzICKMj7tRS+a0NYjaWgSgp+i4F5J0SkdAjtZoUw qs7HR6TtzaLqyQtUBMypJ/bDAUxj0goSWUiAfteC11TC9TCPxiSieZRxwppnEU32Wzjk vdUfNWfqCjGro7qqRdhvOuFyAnSDh1T6z+5i/6d9JsqwNXXKZw9WI92j9JSvBKFLQIi6 wpPZAo+rwsCu81+V9jzDNeNmLUXfiP8zbaH+/+HaK2iDmN1GX+sWGoYg994L/dmwjD7v nm/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694204847; x=1694809647; h=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=LV+tuJCp9H9ddUWpTrZna2FDHSHYOfBxnmXpUVNpo1I=; b=BhIeWaj1A5QwFJ0IKoF6sB0+BxH+2PxUHc7UlKdhuB7PXIzdYMizHtqWPPeD2sQoNK P+tkkz7PTW6/Fwn+Hffxrutewf1Ulyuoc2MuSyAaHxBiZDfslg/j+NjVrEHb4BAn8pSI u8SXlnlTz65Uz/tHIBkSJmB9zuZwHlUco+H/4Zev/sy8XUlfyEyn39VEhx6pZJc3bWAe Blj47guXkjk2rMeZp3mboAAB1QCUEc45itJf6VOBEt9v9/bxyGaJZ7CNtzJCqux7jY4Q k9lAb9oMDvkGheKflioPsDxykngePiJJang+MJLYv3TzWgua3x4PgJJYu7gDdRMzgN+R utLQ== X-Gm-Message-State: AOJu0YwbhvfASWfHwUxJnLsserDrK4azHk3pws5RT3YsCD7ppdmVrwI0 rhh3Qi7+wJ2o9atCGd+xo9hm/es30U6KxRD+WMuZY5/u+tDfGg== X-Google-Smtp-Source: AGHT+IEKqnyRKXLA0z9bCOliohFF1Y6JLbVAdJWdlGllkctgTxR44RJk1uGh6odixYwTd374drzHIKSuPdHmVviKsdU= X-Received: by 2002:a25:407:0:b0:d44:a90b:ba50 with SMTP id 7-20020a250407000000b00d44a90bba50mr3144897ybe.5.1694204847357; Fri, 08 Sep 2023 13:27:27 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Fri, 8 Sep 2023 23:27:10 +0300 Message-ID: Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary="0000000000001a83e20604decefc" Subject: Re: [PHP-DEV] RFC Proposal: Readonly Structs in PHP From: someniatko@gmail.com (someniatko) --0000000000001a83e20604decefc Content-Type: text/plain; charset="UTF-8" Hi internals! I think it can be useful for structured data obtained from decoding JSONs or from a relational database. However, maybe there is an even better idea, which combines the best of two worlds: the world of your structs RFC, and native PHP arrays - something like "array shapes". For instance, taking the syntax from your proposed RFC (but perhaps the keyword changed to `shape`, or perhaps `array class` to reuse keywords - let's not bikeshed here) to define a shape, and adding a function like `array_shape_coerce($array, $shapeType)` which internally marks an array to be of a defined shape if it matches its definition (throws an exception otherwise, or perhaps returns true/false value or an enum for success/failure) and allows it to be passed where a certain shape type is expected. I think this could be a better approach than pure structs because it leaves all array-based PHP machinery available to such "structs", like `array_map()`, `array_filter()` etc. There is an open question of whether array coerced to shapes can be modified, and if yes, how to react to modifications. Perhaps the easiest solution is to have another function along the lines `array_shape_seal($array, $shapeType)` which not only marks array to be of shape (if the given array matches given shape), but also permits only such modifications which keep the array to be of this shape. And if the regular `array_shape_coerce` is used, then any modifications are permitted, but the array "loses" its shape. Regards, Illia / someniatko --0000000000001a83e20604decefc--