Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122849 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id D12071A009C for ; Tue, 2 Apr 2024 09:08:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1712048957; bh=Rp/wrdwobUtoioEPc/pA8oVYhL3sXJQodqjo1arV0io=; h=References:In-Reply-To:From:Date:Subject:To:From; b=YS2Oj3dExJ2XK9zBBPtijX/X0li8ys1HSXIbdmP4m7aentz7xztS0UWtVwG5o4c6T +8CruNjwCyirpdR2BJS/wtEahiPW1AX2ceF7QoCM/ZfpICX2wiz6Dl7PfpXGPobBrE X5XM0Vq58EPWZ6BEqjQhkyPjs6KNb/vO67vhkFgqts8jy/rvTghhYmiFhmtaGVan+d C9hl/zWnJNLuIg8/LOZUdcUDTQNvqP2B4VKTEV1DTGcmcw8RdJy5/bKMGlfL5fc6wr Dl19KJTlevyHJLWkoAe0iHJiyD8x7vcYkT/ATaiYPvFETM3Bb2kFjbHx/ZZT34S3F6 GoZFY7hLBuSFw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id B29C118006D for ; Tue, 2 Apr 2024 09:09:16 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-vk1-f171.google.com (mail-vk1-f171.google.com [209.85.221.171]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 2 Apr 2024 09:09:16 +0000 (UTC) Received: by mail-vk1-f171.google.com with SMTP id 71dfb90a1353d-4d43d602cd6so1546695e0c.0 for ; Tue, 02 Apr 2024 02:08:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712048927; x=1712653727; darn=lists.php.net; h=content-transfer-encoding:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=Rp/wrdwobUtoioEPc/pA8oVYhL3sXJQodqjo1arV0io=; b=aUWvxMSrMOkLHDujskqrXWNjsPhIb9ANHCBywStwapIvbcNUzraNdZYSZ5XP8WSi9V 51LkjQYw7on3HwjVGVSEHNY1eGcZw02sALY9ghPG2zxeRGeLgsKVcWwV4Yksov38jrlH MkHezJYmU2xqmFbVc2fZtFVwLo6pKSU1Gc1UFkQTR0xEAY8Qd9GCYBvX1zmsNDhyX1q5 eQyISI89Osax5hktcIjymmGAw+xKuGBaWhqsC7RN2UhvZ9bcZuO8i1gOwFNjZalKvNns HLCoJXv1wh4gZ7T2vsOebLynQ3txmgRDoAoq4PwrSupPwL+hnHikeO/NiHD9+vIguh2w EtIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712048927; x=1712653727; h=content-transfer-encoding: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=Rp/wrdwobUtoioEPc/pA8oVYhL3sXJQodqjo1arV0io=; b=J+p7RA67nVVpfSFiYjP0XDhZL7HR/nih2vSwQJsguragdqzBQCmF/GDjHFYWNQXS5O EX5sreRUW+WlE+IDXVzaN3/XUuQWFTvv9iiev6mfYUS597RCwN9T/xbvXWdGNZFPLZEG rIUdHLlS/ClMz67BulGLBeye+/aEE+U9VWvWJ5YLXjgaAQuRui+ejqKCmHwvU+geqBwB HK6uZLJrBacw7vrGiQnPidt+va5TORdRs5NQHqXViXgXsKDihtoHBRf7GLvHn/pIjlHo uoA1gnp+afVZBhLRYtOuzcab+V2rczS57tni4MmorkWYN+KjFPq0cmnonyGviNdrLFqY NjFg== X-Gm-Message-State: AOJu0Yy40k0IKyLJwv3NnfS3PQgaNcvfDm87Uu2EDemEzXNuQW7EM9/P O+sJISG2S5swRcquIvkG8HbG12NJmMcLY3NHnb0jqa2aHslnDe4dM/wCSuorXT2wt4Cd/oy9hwk nv4e+FtlMALUpq5GC/+3fxzqPYVEGbTd8EggT7A== X-Google-Smtp-Source: AGHT+IGWS26qfAfv7rJlWWYOHjYbo4JLdFHiYfob7L5qirRlA9wD85JiWVcMYRjElgpBo2+9nzjZciHJ25HIKAygmww= X-Received: by 2002:ac5:ca0b:0:b0:4d4:1ce7:c5f0 with SMTP id c11-20020ac5ca0b000000b004d41ce7c5f0mr7153346vkm.8.1712048927156; Tue, 02 Apr 2024 02:08:47 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 2 Apr 2024 11:08:36 +0200 Message-ID: Subject: Re: [PHP-DEV] [RFC][Concept] Data classes (a.k.a. structs) To: PHP internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable From: tovilo.ilija@gmail.com (Ilija Tovilo) Hi Marco On Tue, Apr 2, 2024 at 2:56=E2=80=AFAM Deleu wrote: > > > > On Mon, Apr 1, 2024 at 9:20=E2=80=AFPM Ilija Tovilo wrote: >> >> I'd like to introduce an idea I've played around with for a couple of >> weeks: Data classes, sometimes called structs in other languages (e.g. >> Swift and C#). >> >> snip >> >> Some other things to note about data classes: >> >> * Data classes are ordinary classes, and as such may implement >> interfaces, methods and more. I have not decided whether they should >> support inheritance. > > I'd argue in favor of not including inheritance in the first version. Tak= ing inheritance out is an impossible BC Break. Not introducing it in the fi= rst stable release gives users a chance to evaluate whether it's something = we will drastically miss. I would probably agree. I believe the reasoning some languages don't support inheritance for value types is because they are stored on the stack. Inheritance encourages large structures, but copying very large structures over and over on the stack may be slow. In PHP, objects always live on the heap, and due to CoW we don't have this problem. Still, it may be beneficial to disallow inheritance first, and relax this restriction if it is necessary. >> * Mutating method calls on data classes use a slightly different >> syntax: `$vector->append!(42)`. All methods mutating `$this` must be >> marked as `mutating`. The reason for this is twofold: 1. It signals to >> the caller that the value is modified. 2. It allows `$vector` to be >> cloned before knowing whether the method `append` is modifying, which >> hugely reduces implementation complexity in the engine. > > I'm not sure if I understood this one. Do you mean that the `!` modifier = here (at call-site) is helping the engine clone the variable before even di= ving into whether `append()` has been tagged as mutating? Precisely. The issue comes from deeper nested values: $circle->position->zero(); Imagine that Circle is a data class with a Position, which is also a data class. Position::zero() is a mutating method that sets the coordinates to 0:0. For this to work, not only the position needs to be copied, but also $circle. However, the engine doesn't yet know ahead of time whether zero() is mutating, and as such needs to perform a copy. One idea was to evaluate the left-hand-side of the method call, and repeat it with a copy if the method is mutating. However, this is not trivially possible, because opcodes consume their operands. So, for an expression like `getCircle()->position->zero()`, the return value of `getCircle()` is already gone. `!` explicitly distinguishes the call from non-mutating calls, and knows that a copy will be needed. But as mentioned previously, I think a different syntax offers additional benefits for readability. > From outside it looks odd that a clone would happen ahead-of-time while t= alking about copy-on-write. Would this syntax break for non-mutating method= s? If by break you mean the engine would error, then yes. Only mutating methods may (and must) be called with the $foo->bar!() syntax. Ilija