Newsgroups: php.internals
Path: news.php.net
Xref: news.php.net php.internals:125976
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 74DE51A00BD
	for <internals@lists.php.net>; Sun, 17 Nov 2024 00:16:02 +0000 (UTC)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail;
	t=1731802719; bh=mRtBYFmgo8LgpAPGDPLodY49x/NZvpd5pOke7OD8I90=;
	h=References:In-Reply-To:From:Date:Subject:To:From;
	b=kv5X5qz7CjOqzQYZkUyO0GD6CCcKUY/X5biUXRmhBZmb9/qdzZqHhwhzBaN0WrXM0
	 jD8/PYlAQujD/4B/7Dh7MI7IJZQXn8TOAd9Hv50rRLMBr+InfHB5Yz8FsQeg6BbluU
	 i+uSiL793ozdHYRbLUgXPuat2vq2KJGoVnlWIcuBam0elfMbdZMFngyhAgk2bGWZFm
	 CWskdGOv1MnY8pIADMxFsoAQtEtIEiKHHmX+Sn8nkt2bG5rsAvby+W4aBpZkKcyGdA
	 Tnf0P5gCNaVfsIKgUma9bBPd5Yjn4jyvObuLKoUxxEmi6rYayaYy8pxaLnkv4MffYr
	 f/OTKsBkVgsng==
Received: from php-smtp4.php.net (localhost [127.0.0.1])
	by php-smtp4.php.net (Postfix) with ESMTP id 9B2E5180037
	for <internals@lists.php.net>; Sun, 17 Nov 2024 00:18:38 +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,
	RCVD_IN_VALIDITY_CERTIFIED_BLOCKED,RCVD_IN_VALIDITY_RPBL_BLOCKED,
	RCVD_IN_VALIDITY_SAFE_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=no
	autolearn_force=no version=4.0.0
X-Spam-Virus: No
X-Envelope-From: <tovilo.ilija@gmail.com>
Received: from mail-qv1-f42.google.com (mail-qv1-f42.google.com [209.85.219.42])
	(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 <internals@lists.php.net>; Sun, 17 Nov 2024 00:18:37 +0000 (UTC)
Received: by mail-qv1-f42.google.com with SMTP id 6a1803df08f44-6d419832bf5so3775606d6.1
        for <internals@lists.php.net>; Sat, 16 Nov 2024 16:16:00 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20230601; t=1731802559; x=1732407359; 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=mRtBYFmgo8LgpAPGDPLodY49x/NZvpd5pOke7OD8I90=;
        b=kOB7QsoPPh9OUTRuHbKr+fDj/8WMWw6BbzHeQ3+nu4Ivkatw2TYYPPdnEBd3sHqHOU
         317Sqa3zczsZYg4d+c6DeCHq/yzufeJ2sUG6prq+VHh9f6PXLpBGjBT2V80M9WgxgTzB
         I+VYWbsEeNPvO7fYlwa5TNEN+uXEzQ7WR69i47wD7g+murYFgPtXEaumA7GqAvwlo4Ep
         AJjac7i3pqVM8uXH16+RbFrfnycBF6KJDlxT8HkGGWa5tpw/f7JzHlSfC5SzHKBtxue2
         mhmwulf7eATQrYZKZT/9Tknakiyz2VAOPqB61+0rid1ZZ+h7/7L4A27lklWEI+isMXN7
         tn5Q==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20230601; t=1731802559; x=1732407359;
        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=mRtBYFmgo8LgpAPGDPLodY49x/NZvpd5pOke7OD8I90=;
        b=ZHBmJwu6xclsmB6v0lLuBnYoZ5xK8E45SLRRcr7vOmZQGgYA9ofcOSnjzYWnEM0Pge
         DJunEBbjjsxzwFiq+wY/xJd83uwzDs2F1MkcHgFnImC+AcvLzMCXe0pm3vuv4sKc4tmX
         CazPM8l6bnxqe/O0cH+2nMcp1VZErmlrM7be9MOaAuAcw7oOQYIshi6DiDT32+rJH+Vb
         ktcy3sv6XlVX9W5No77DH1Vc1b1ooF2gkec/JExXlAVn9Z1n91tMaAbePvRzSe+V8u5q
         pfVuZzimi3wwdP44dgOkOdrs9LOC91zm45hq7IMsmdD3jtubqI0wh8V1rzfC56RmNsnn
         bNfQ==
X-Gm-Message-State: AOJu0YwzW/E6tRKLldi8RDXuQ7hsyaOfipme9RM80KSCy4R2OyftwWOL
	loc80U0Xj8bGgWWvbdfWfGa2RzUF4Szc5t1gE82pHkAAjiL5h31PuXmqCWrYZkZZu/JEm+yXhL5
	nbbFjIZQEbqW/JLt5fon2F6iMhZuU/OT/vPY=
X-Google-Smtp-Source: AGHT+IEWWu9s0ycRxSqWF/DoFGmeSWJVKKiwwSVWU+vr7TGE/oRqKdiB6jYhfGsV06njv88MFKR5D72x4ydqQqPCNdg=
X-Received: by 2002:a05:6214:5401:b0:6d1:9f1b:587c with SMTP id
 6a1803df08f44-6d3fb8c8683mr109531846d6.46.1731802557859; Sat, 16 Nov 2024
 16:15:57 -0800 (PST)
Precedence: bulk
list-help: <mailto:internals+help@lists.php.net
list-unsubscribe: <mailto:internals+unsubscribe@lists.php.net>
list-post: <mailto:internals@lists.php.net>
List-Id: internals.lists.php.net
x-ms-reactions: disallow
MIME-Version: 1.0
References: <d603f372-5f58-4308-aba1-151c35f3aa94@app.fastmail.com>
In-Reply-To: <d603f372-5f58-4308-aba1-151c35f3aa94@app.fastmail.com>
Date: Sun, 17 Nov 2024 01:15:47 +0100
Message-ID: <CAPyj-LCtzF6FKwGakaouyueaLRQsu=B4uASFZKEG4x+-a2J2iA@mail.gmail.com>
Subject: Re: [PHP-DEV] RFC: Records
To: PHP internals <internals@lists.php.net>
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
From: tovilo.ilija@gmail.com (Ilija Tovilo)

Hi Rob


On Sun, Nov 17, 2024 at 12:15=E2=80=AFAM Rob Landers <rob@bottled.codes> wr=
ote:
>
> Hello internals,
>
> I'm ready as I'm going to be to introduce to you: "Records" https://wiki.=
php.net/rfc/records!

Thanks for your proposal. I very much agree that value semantics are a
highly desirable feature. I sent out my concept proposal on how this
may be achieved through structs some months ago. [1] There are some
remaining technical challenges, but the PoC looks promising. [2]

I personally do not think immutable data structures are a good
solution to the problem, and I don't feel like we need another,
slightly shorter way to do the same thing. In my proposal, I mentioned
that there are two primary issues with immutable data structures:

1. They are expensive. Any modification of the class requires a clone
of the object by design, even when the cloned object is immediately
discarded. This is most noticeable for lists and other growable data
types, because they are large and change frequently. An O(n) insertion
becomes infeasible very fast.
2. The clone is explicit, which gets old pretty quickly. Many of PHPs
quality-of-life features like op-assign (e.g. +=3D) become unusable.

Structs would instead be fully-fledged objects that adopt CoW
(copy-on-write) semantics, which is how PHP already implements arrays.
Essentially, objects are automatically cloned when they are changed
_and_ when the object is referenced from multiple places. If the
object is not referenced anywhere else, it may be safely mutated
without a clone, as there is nobody to observe this side-effect. When
a shared object is mutated multiple times (e.g. an append to a list in
a loop), only the first mutation requires a clone. It's also something
you don't need to think about, it just happens.

As mentioned, the main motivation of my proposal is to implement new
dedicated data structures like Vectors, Maps, Sets, etc. in PHP
(userland and extensions) without sacrificing ergonomics or
performance. However, they would also solve the same challenges for
simple data objects. IMO, immutability was never a great solution to
the problem of "spooky action at a distance". Mutability itself is not
a problem, just shared mutability.

Note that this concept is heavily inspired by Swift, Rust and likely
other languages I don't know. Chris Lattner (the original author of
LLVM and Swift) has shared very similar thoughts in an interview
recently. [3]

If the primary motivation of your RFC is to reduce boilerplate, then
it's true that structs would not help much. I do wonder whether 1. we
need even shorter code and 2. if we do, whether it's something that
could be added to classes alike, rather than tying it to records when
it could be more generic instead.

A small note: The $test =3D &Test(); syntax is ambiguous, as it's
already legal. https://3v4l.org/CE5rt

Ilija

[1] https://externals.io/message/122845
[2] https://github.com/php/php-src/pull/13800
[3] https://youtu.be/JRcXUuQYR90?si=3Dp51_x3wkfeeeGfq-&t=3D3319