Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121026 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 72627 invoked from network); 9 Sep 2023 07:14:16 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 9 Sep 2023 07:14:16 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 3AC181804B0 for ; Sat, 9 Sep 2023 00:14:15 -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-oo1-f46.google.com (mail-oo1-f46.google.com [209.85.161.46]) (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 ; Sat, 9 Sep 2023 00:14:14 -0700 (PDT) Received: by mail-oo1-f46.google.com with SMTP id 006d021491bc7-570e63f5224so2004656eaf.0 for ; Sat, 09 Sep 2023 00:14:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1694243654; x=1694848454; darn=lists.php.net; 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=4/xtesfxbsg6A3BHmoRuQwv+cpO9xg+Xtof3eMx/uVU=; b=dO8dPCj+XoXP73p6Eb+AuRXw/QIxiiDKT3bBINVhrPwwX+5D2oa+vhyVfh9lq0elmO CvQ3aJlpPzFzTghJxYjZmbS5XmKlckJmDZu7kv0MsdPH69O4y2GcFX09nsUQj6mGaEbj 2bOiC0CJrssF+FFtBVvRtW7XwnPmsLo2zvkTjkNtCs0SunPW4kGc9BEroe8oOcWwK3Co F+iEr9rtj5XrjbneveS1cW1YlTbjVOL/tEVx0ki54uZj4g6BRFanHcw0MLdrYwza1VEw Cb1UCoNR+q6TmHPuHPf2MSgDqPXU6XzjDYw45ctUu5yj1GDNqqyuUVNc5YTy/nTZPtqx hriw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694243654; x=1694848454; 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=4/xtesfxbsg6A3BHmoRuQwv+cpO9xg+Xtof3eMx/uVU=; b=ghHbqb0Rslz6rcVTcvT4yS7OiCNpXNr697ry0AwsLg6DZfMYtEWy+9tUEvn+/UFcrf vShD5uI1OAi+6EJ1qh2STNivmwnpXgCjDTpVDrbyy8CM5IEVgPKepN69a7S7jjeU7H6b roanbC6vREARBJEqK9PwJSyxk6jTvaegjou7wtD9bhIqgaAro/CYIZCVsBK0t3ysDwI0 ADlHZFfypqywpqDs2NOr/uudCcy9rzY44Ut1yaLgMbGQeh+Qsxh7TyrDbJeov83W/JMW pe1DK/GureHGXWMkq/FVt59xw/n2e51IIoK/0jIUtKrrKroHgA6b1XRWKnOnOJH6Wmt+ ZPvQ== X-Gm-Message-State: AOJu0YzWNRWj6CehdGaHuH16dilYNIGjZys/zzAkjSDGAQ/roMudnXob kdxIgCm8/RksMo96Y84ZGBuobZNMgaSilHfyvQQ= X-Google-Smtp-Source: AGHT+IGLzEuoXvcojEmPSa40wm31cHoa9EtBa+50ilWhyLki+nXYjEhmNDfr7hsa7zzRfSo3/msrcxozbjx4TrHQcZc= X-Received: by 2002:a05:6870:f14c:b0:1b0:3f7f:673a with SMTP id l12-20020a056870f14c00b001b03f7f673amr2225147oac.25.1694243653973; Sat, 09 Sep 2023 00:14:13 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Sat, 9 Sep 2023 09:14:02 +0200 Message-ID: To: David Gebler Cc: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] RFC Proposal: Readonly Structs in PHP From: landers.robert@gmail.com (Robert Landers) On Sat, Sep 9, 2023 at 1:09=E2=80=AFAM David Gebler = wrote: > > On Fri, Sep 8, 2023 at 2:12=E2=80=AFPM Lanre Waju wrote: > > > Dear PHP Internals, > > > > I am writing to propose a new feature for PHP that introduces the > > concept of structs. This feature aims to provide a more concise and > > expressive way to define and work with immutable data structures. Below > > is a detailed description of the proposed syntax, usage, and behavior. > > > > Syntax > > > > struct Data > > { > > string $title; > > Status $status; > > ?DateTimeImmutable $publishedAt =3D null; > > } > > The Data struct is essentially represented as a readonly class with a > > constructor as follows: > > > > > > readonly class Data > > { > > public function __construct( > > public string $title, > > public Status $status, > > public ?DateTimeImmutable $publishedAt =3D null, > > ) {} > > } > > > > These as the most basic examples of what you're proposing are barely > different expressions in length, let alone anything else. I wouldn't say > the first example is particularly more helpful, more readable, more conci= se > or conceptually more expressive than the second. The rest of your example= s > are similarly either very minor savings of a few characters shaved off he= re > and there, or just as verbose as instantiating a class with a constructor > anyway. > > I'm not convinced by the rationale that this would be a new feature > worthwhile for improved expression or concision. I can see a potential > benefit in these "structs" (not sure that's the term//keyword I'd choose, > given it has different meanings in other languages) though, as a kind of > template for properly structured arrays, with a built-in ability to cast > them to arrays or treat them as arrays/iterables. This would potentially > help give some of the power we're missing by not having generics. So it > wouldn't just be equivalent to a shorthand for a readonly class with a > constructor, but one which also satisfied a couple of interfaces with the > implementation automatically provided. > > That's my initial reaction/two cents. > > -Dave Indeed, When I first saw this, I saw them as typed-arrays, and it would be great if they were treated as such. Essentially, type-check, then stored in a regular array and as far as the engine is concerned, that's what they are (meaning they could be used when a library expects an array, or gradually added to legacy code that passes around opaque arrays). This could add a tremendous amount of expressibility to PHP. This would mean a struct with the same properties and values are equal. It'd be even awesome if you could cast between structs (and to arrays), as long as the struct/array being casted from contained all the right properties/values. For example: $myStruct1 =3D (MyStruct1) $myStruct2; // myStruct2 could even be an array, or another struct is the same as: $myStruct1 =3D new MyStruct1(...$myStruct2); except ignoring extra properties/values. Maybe the casting could even "hide" the extra properties in a private variable so that if it is casted to an array, or serialized as json, or casted to a different struct, the original data shows up. As far as being able to increase the expression of regular 'ole PHP, it would allow libraries to get rid of opaque arrays with hidden options (looking at you Guzzle), and essentially not change but a few lines of code, while making it easy for users to correctly use the library. Anyway, just thinking out loud. I have no idea if the original author has an intent for any of this, but it could be awesome. Robert Landers Software Engineer Utrecht NL