Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:126036 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 3C6341A00BD for ; Sat, 23 Nov 2024 17:34:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1732383418; bh=vXwqVNZvIoCiZSVi/YBEWGq8e3hedxWIq+Tth9WAB4w=; h=Date:Subject:To:References:From:In-Reply-To:From; b=C3QjK2DvSBnu2tcWZyGDdyrJYTR917KkslnFClwVACeznyOudKicn3YYyMk+A8wTP Dr/H+zLAsSeaTJGhWzLcHEU/M1EPrln+kukTP1KoO1nK/+cK9Z7TwUSeu1SL64SBHe xr/nhAI6zOyeRiHgiODZIN5N2Dn9r+N5Bn/lM7pw7zkNalx2/fvGfCYNUY4E5agNxE lCB8hSmOQBrL4U7S2y9E4p0oll4YJr1b6gszhKkq1cPhC8ZKQmsW1yy6g3he2EMzeb Bdnm9KZLlRSWXapJ9MFdKaBtKtaFf8bf7JpQFxRNVE11lZdA4bRuLMnwznZcXAGzVd TbXvYBIrOalUA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 4D5BB180077 for ; Sat, 23 Nov 2024 17:36:57 +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.1 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,HTML_MESSAGE, RCVD_IN_DNSWL_LOW,SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from fhigh-a1-smtp.messagingengine.com (fhigh-a1-smtp.messagingengine.com [103.168.172.152]) (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 ; Sat, 23 Nov 2024 17:36:56 +0000 (UTC) Received: from phl-compute-08.internal (phl-compute-08.phl.internal [10.202.2.48]) by mailfhigh.phl.internal (Postfix) with ESMTP id 88D1D11400C9 for ; Sat, 23 Nov 2024 12:34:14 -0500 (EST) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-08.internal (MEProxy); Sat, 23 Nov 2024 12:34:14 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rwec.co.uk; h=cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm1; t=1732383254; x=1732469654; bh=0RLjpBnisU 2My4ukhZ/o5Fhde+9AHQ93gnnWrz2MLr0=; b=IO83RkXsZsfY7WvPbMi1EN/jV3 2Nx85VzV8Ulb77v3JysfnIxJpEBQ8uh1ggP7inRNfLpvCDKD0ReltMpVnqPOmLh1 t/e+jZqOUr+SJsyEUrn+49EvllQ2f/11WxTpsL6UMDd8jp+Hh4js3QeR1ILab50a JyIWU/Vz9GZ3t65U04DhRCye2i8GJaLY1yoG68GtCQkF9woE+bP44qFn720qiEtk ianhfE6rLRkbQzgobUwiimBRvPQgUND4m+Z/OSgRvFa8ybzCT5z2tZKL9wFOxtIy K6oeNyEB2OgvfXkPy91jvZGkPWPAHuYIc5Fck+nBfhTsVJU/LnwSVe/jJcfg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t= 1732383254; x=1732469654; bh=0RLjpBnisU2My4ukhZ/o5Fhde+9AHQ93gnn Wrz2MLr0=; b=EccppS+OjDTPABuk9oXrmiyhJ3+ZvDn4++a//mUA0eX4Ff91MYS Osc1k1spEdASFaiKDrf+0/rr6drdSSz8HUF4v137aL7A0G+VIm9aTENLuVjOhKCF WdsoGv3pt1UY00U0JphiS5kDKZ7cth2twYNdWq/Okc2uyiCQuGhTbReqW+OLnlSZ sGK4OEYbh5jv5B+C/CU2NV7D6zxOEX6S3Yo3q0jU/YTthT3LNq/k8r/Jt6A6EGGV BbTOHyWz4151kfEFDbU7Da2cu++rDseohtas/t9kLwUyQ4rmI1jCluwb/+Povhu3 +iFsAX6L6mM27UQwC59M7v5bUTEgGkAMzNg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefuddrgedugddutdduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucenucfjughrpegtkfffgg gfuffvfhfhjgesrgdtreertddvjeenucfhrhhomhepfdftohifrghnucfvohhmmhhinhhs ucglkffoufhorfgnfdcuoehimhhsohhprdhphhhpsehrfigvtgdrtghordhukheqnecugg ftrfgrthhtvghrnhepheetleeiiefgueduieeuieffvdevheduueefkeejuefgffeftdei tdegtedtleetnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrh homhepihhmshhophdrphhhphesrhifvggtrdgtohdruhhkpdhnsggprhgtphhtthhopedu pdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehinhhtvghrnhgrlhhssehlihhsth hsrdhphhhprdhnvght X-ME-Proxy: Feedback-ID: id5114917:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Sat, 23 Nov 2024 12:34:13 -0500 (EST) Content-Type: multipart/alternative; boundary="------------4bOMJ0ijVE7C00GtJtjbnVyY" Message-ID: <2c286462-a9ac-4b12-89bf-aea66680dea0@rwec.co.uk> Date: Sat, 23 Nov 2024 17:34:11 +0000 Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PHP-DEV] [RFC] Data Classes To: internals@lists.php.net References: <18b85ba5-5f1c-489c-9096-3ae203977fbe@app.fastmail.com> <163a08c1-2279-4a3f-a1bb-8cdfe406a4ba@rwec.co.uk> <0b87a3eb-7e63-4e28-a11d-25824e240a73@app.fastmail.com> Content-Language: en-GB In-Reply-To: <0b87a3eb-7e63-4e28-a11d-25824e240a73@app.fastmail.com> From: imsop.php@rwec.co.uk ("Rowan Tommins [IMSoP]") This is a multi-part message in MIME format. --------------4bOMJ0ijVE7C00GtJtjbnVyY Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 23/11/2024 16:05, Rob Landers wrote: >> Your RFC doesn't discuss this - the changeName example shows behaviour >> *inside* the method, but not behaviour when *calling* it > > An interesting observation, can you explain more as to what you mean? Looking closer, there's a hint at what you expect to happen in your Rectangle example: $bigRectangle = $rectangle->resize(10, 20); assert($bigRectangle !== $rectangle); // true It seems that modifications to $this aren't visible outside the method, creating a purely local clone, which would be discarded if it wasn't returned (or saved somewhere). I can see the logic, but the result is a bit unintuitive: data class Example {    public function __construct(public int $x) {}    public function inc(): void {      $this->x++;   } } $foo = new Example(0); $foo->x++; $foo->inc(); echo $foo->x; // 1, not 2 I think it would be clearer to prevent direct modification of $this: data class Example {    public function __construct(public int $x) {}    public function inc(): void {       $this->x++; // ERROR: Can not mutate $this in data class    }    public function withInc(): static {       $new = $this; // explicitly make a local copy of $this       $new->x++; // copy-on-write separates $new from $this       return $new;    } } That would still be compatible with Ilija's suggestion, which was to add special "mutating methods": data class Example {     public function __construct(public int $x) {}     public mutating function inc(): void {         $this->x++;     } } $foo = new Example(0); $foo->x++; $foo->inc!(); // copy-on-write triggered *before* the method is called echo $foo->x; // 2 -- Rowan Tommins [IMSoP] --------------4bOMJ0ijVE7C00GtJtjbnVyY Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit
On 23/11/2024 16:05, Rob Landers wrote:
Your RFC doesn't discuss this - the changeName example shows behaviour
*inside* the method, but not behaviour when *calling* it

An interesting observation, can you explain more as to what you mean?

Looking closer, there's a hint at what you expect to happen in your Rectangle example:

$bigRectangle = $rectangle->resize(10, 20);
assert($bigRectangle !== $rectangle); // true

It seems that modifications to $this aren't visible outside the method, creating a purely local clone, which would be discarded if it wasn't returned (or saved somewhere).

I can see the logic, but the result is a bit unintuitive:

data class Example {
   public function __construct(public int $x) {}
   public function inc(): void {
     $this->x++;
  }
}
$foo = new Example(0);
$foo->x++;
$foo->inc();
echo $foo->x; // 1, not 2


I think it would be clearer to prevent direct modification of $this:

data class Example {
   public function __construct(public int $x) {}
   public function inc(): void {
      $this->x++; // ERROR: Can not mutate $this in data class
   }
   public function withInc(): static {
      $new = $this; // explicitly make a local copy of $this
      $new->x++; // copy-on-write separates $new from $this
      return $new;
   }
}


That would still be compatible with Ilija's suggestion, which was to add special "mutating methods":

data class Example {
    public function __construct(public int $x) {}
    public mutating function inc(): void {
        $this->x++;
    }
}
$foo = new Example(0);
$foo->x++;
$foo->inc!(); // copy-on-write triggered *before* the method is called
echo $foo->x; // 2


-- 
Rowan Tommins
[IMSoP]
--------------4bOMJ0ijVE7C00GtJtjbnVyY--