Newsgroups: php.internals
Path: news.php.net
Xref: news.php.net php.internals:125362
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 A5E5F1A00BD
	for <internals@lists.php.net>; Fri, 30 Aug 2024 14:15:30 +0000 (UTC)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail;
	t=1725027445; bh=CqRlFKKHBBap4KH294qdtQehwSHX9p1HTnRO6TPvcU0=;
	h=Date:From:To:In-Reply-To:References:Subject:From;
	b=gTFB7h8AUtheSimsUgPq3qQGg1UoFUzkaFjVT31Dnd5ixYmSnIMoEe0i/9Tu+V19k
	 +9z5Nj20Q0BkRr4rPDNrUEKoONuy1iUKRQpaVv0o9R30f0CBSfUfv1iob6HGGCzqPy
	 R9/sV7NGY0jp0LsOvnmCOhs5zmZMxy9lu82KXMLyhpW/v13HecF7Tw3thq/xOXHcXC
	 6fTKPkXe1Eh/8gJD09ZvzbmgaQbr2R+QI83syYXT/lMkY0eRHXS5WZ7NVMNAgt6RK2
	 JYwigF8UtIFVNxIoNmWmJ5xlV9/bbdxJtTy30SHPmU2cOetvKapGWxBiCUHRX0n233
	 alQngh3E6zDdQ==
Received: from php-smtp4.php.net (localhost [127.0.0.1])
	by php-smtp4.php.net (Postfix) with ESMTP id 3D64F180059
	for <internals@lists.php.net>; Fri, 30 Aug 2024 14:17:24 +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,RCVD_IN_DNSWL_LOW,
	SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=4.0.0
X-Spam-Virus: No
X-Envelope-From: <larry@garfieldtech.com>
Received: from fout3-smtp.messagingengine.com (fout3-smtp.messagingengine.com [103.168.172.146])
	(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>; Fri, 30 Aug 2024 14:17:23 +0000 (UTC)
Received: from phl-compute-03.internal (phl-compute-03.nyi.internal [10.202.2.43])
	by mailfout.nyi.internal (Postfix) with ESMTP id 088161380255
	for <internals@lists.php.net>; Fri, 30 Aug 2024 10:15:28 -0400 (EDT)
Received: from phl-imap-06 ([10.202.2.83])
  by phl-compute-03.internal (MEProxy); Fri, 30 Aug 2024 10:15:28 -0400
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=
	garfieldtech.com; h=cc:content-transfer-encoding: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=1725027327; x=1725113727; bh=1OHLKdHIcSHfkXz54y3Gz
	YDZgCUBT2hRwKNK1bQkCvE=; b=VHpUJo8Wu/D0Kwm6zx9569Ezzd625vnIARGAe
	FZIioKmtP/vmEMKF38MdVcKFFqlc+TnTZ5aL/jiOI+dGcxiCx+MQRany7rgzf5dd
	PjBdMtqzkVSXBK3nnpxwVwo5Ig7DfJ9ZWWWi+eB5P3TBngP1J4GXo5DN2JqZnSLc
	TSf9Ae0hfR7SaoJ8gY20imIC+5uJAGJ3t6E91o2biggs8QSuuWMGuTZ4WFdDzsky
	icbX1it7w8QjRUlOXa1Z5k2EKLf8WQTQFbhHxoPmVnQEumzSm8sDhM4NZ2AFMPqh
	hRBKz7f9CpAXgSyZcFOKMrIK/EehtghVOhE2QY90uLp4VK+Xw==
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=
	messagingengine.com; h=cc:content-transfer-encoding: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-proxy
	:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1725027327; x=
	1725113727; bh=1OHLKdHIcSHfkXz54y3GzYDZgCUBT2hRwKNK1bQkCvE=; b=b
	t94fM3/XpMyO/fwzNhqIP1Pv3EvTf6aVUAmYd7VLVfFczW8kghYRjAN1rqj89TTz
	DVX8BjoeV0ZA/Zkd7/9PlxRVYjk0tq8vgJ7z7mN9UKuwP5ZVywpNPcF27Y/wnayn
	AuFJ0ksXRDihCSCP6J//lLMI+RVuHJFHTfHZa2Wc86lMMZX0jIbD+gJGVsJESpE0
	BkH0e3ZPAHVUU7c7yk5cqb+F7Ad7rRk8cPMsU7cbVYz+EAOmiO7cbB2wrNT8GIh2
	uW3Y6KJD89R1OyfS/1PJuK8fq25SFxc5KwfnS2SBW+iyoxKMUlOe9Twf8UhD8AMH
	2MxeaEkduMzX/H7fyJm8Q==
X-ME-Sender: <xms:_9PRZvMmW_XQ4G3Gwknfmrss0WfntrcND6TSaPSMhEzVeMvl_sYrjw>
    <xme:_9PRZp8DyEZ5Iua23AuWz1RQj_sNZ_ZgC9i_oDJ0exkYgL5SFt6jqbAnxHznSXFmU
    EuZdeMRPO6wPA>
X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrudefiedgjeefucetufdoteggodetrfdotf
    fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu
    rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh
    htshculddquddttddmnecujfgurhepofggfffhvffkjghfufgtgfesthhqredtredtjeen
    ucfhrhhomhepfdfnrghrrhihucfirghrfhhivghlugdfuceolhgrrhhrhiesghgrrhhfih
    gvlhguthgvtghhrdgtohhmqeenucggtffrrghtthgvrhhnpeeugfetieejueevffdulefh
    hfethfekvedtueevgfffvdefiedvtefgheevteelffenucffohhmrghinhepphhhphdrnh
    gvthenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehl
    rghrrhihsehgrghrfhhivghlughtvggthhdrtghomhdpnhgspghrtghpthhtohepuddpmh
    houggvpehsmhhtphhouhhtpdhrtghpthhtohepihhnthgvrhhnrghlsheslhhishhtshdr
    phhhphdrnhgvth
X-ME-Proxy: <xmx:_9PRZuS8Xgb02wuSSLkwakoSdiALpjmx6FkA6C0XLthN-8bo2g9ChQ>
    <xmx:_9PRZjuxHvMGdQxN9BENCuqStNa7F2dxxtEg_ckmntSkUP5-5HY5wg>
    <xmx:_9PRZnc89N-gWIRcttEMROUCK6pON6nTqOf895yw-EFqKBrwKO1kaw>
    <xmx:_9PRZv1E6zm0SSSeIgh-DoeI73sZT-yWTTwELeT2RJ_TDKdlr7Z0Xg>
    <xmx:_9PRZlprxxAXXAEX6SmGKgF8Bgy_adPlfqGUknRBl8Y0RSZneW1hdkqk>
Feedback-ID: i8414410d:Fastmail
Received: by mailuser.nyi.internal (Postfix, from userid 501)
	id 90EF629C006A; Fri, 30 Aug 2024 10:15:27 -0400 (EDT)
X-Mailer: MessagingEngine.com Webmail Interface
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
Date: Fri, 30 Aug 2024 09:15:06 -0500
To: "php internals" <internals@lists.php.net>
Message-ID: <dc51b64f-ec13-44d9-beca-e01219df3264@app.fastmail.com>
In-Reply-To: 
 <CABdc3WpCjh-P9jiqMbUT_ecc4BechT3GnJiFJJxS8Aw5nsA5SQ@mail.gmail.com>
References: 
 <CANSNCr1CdKLwmJbMNw7kKP3pA0F+njwohm6xbTMHZxgmda+bHw@mail.gmail.com>
 <CABdc3WpCjh-P9jiqMbUT_ecc4BechT3GnJiFJJxS8Aw5nsA5SQ@mail.gmail.com>
Subject: Re: [PHP-DEV] [Discussion] Implementing interfaces via traits
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable
From: larry@garfieldtech.com ("Larry Garfield")

On Fri, Aug 30, 2024, at 1:09 AM, Micha=C5=82 Marcin Brzuchalski wrote:
> Hi Brent,
>
> wt., 27 sie 2024 o 09:28 Brent Roose <brent.roose@jetbrains.com> napis=
a=C5=82(a):
>> Good morning internals
>>=20
>>=20
>> I=E2=80=99d like to test the waters about an RFC idea: allowing trait=
s to implement interfaces, and consequently a class that uses such a tra=
it will automatically implement the interface as well.
>>=20
>>=20
>> The original idea comes from Rust, where traits can be used as types.=
 I read a very inspiring post suggested by Larry, on the topic of =E2=80=
=9Cclassic inheritance=E2=80=9D vs the way Rust and Go approach it [1]. =
The tl;dr is that both Rust and Go solve several pitfalls of classical i=
nheritance (the diamond problem and inheritance abuse), thanks to a much=
 simpler approach. In Rust=E2=80=99s case that is by using traits. If yo=
u have the time, I highly recommend reading that post, it=E2=80=99s supe=
r interesting and it gives a lot of good arguments for rethinking inheri=
tance.
>>=20
>>=20
>> Back to PHP, using traits as types seems impossible, since traits are=
 a compile-time copy/paste mechanism, which means there=E2=80=99s no typ=
e information available about them at runtime.
>>=20
>>=20
>> However, allowing traits to implement interfaces would solve this pro=
blem: these interfaces would be copied over to classes during compile-ti=
me, and the interface=E2=80=99s type information is available at runtime=
. On top of that, traits already have well-defined rules for conflict re=
solution, so we wouldn=E2=80=99t need any additional syntax to handle ed=
ge cases.
>>=20
>>=20
>> Even though PHP traits differ from Rust, PHP developers already seem =
to like the idea of being able to =E2=80=9Cattach a type to a trait=E2=80=
=9D one way or another. Let me name a couple of things that happen today:
>>=20
>>=20
>>  =E2=80=A2 Laravel often provides =E2=80=9Cdefault implementations=E2=
=80=9D for their interfaces via a trait [2]. As mentioned before, traits=
 already deal with conflict-resolution, so method collisions aren=E2=80=99=
t a blocker.
>>=20
>>  =E2=80=A2 Both PHPStan and Psalm have an annotation that forces trai=
t users to implement an interface [3], which is essentially the feature =
I=E2=80=99m describing, albeit via docblock annotations instead of prope=
r syntax.
>>=20
>>  =E2=80=A2 Even though it was not accepted, the interface default met=
hods RFC approached the problem from a different angle [4]. While a majo=
rity disagreed that interfaces should implement their own methods direct=
ly, I remember it was a heavily debated topic, and believe that approach=
ing it from the other side might be easier to accept.
>=20
> With the recent RFC proposal for Default Expressions=20
> <https://wiki.php.net/rfc/default_expression> [5], I believe it=20
> presents an excellent opportunity to revisit the Interface Default=20
> Methods proposal. The Default Expressions RFC addresses similar=20
> functionality and, when combined with an opt-in feature flag, could=20
> resolve many concerns raised during the previous discussion.

I... have no idea why you're conflating the Interface Default Methods RF=
C and the Default Expressions RFC.  They have nothing to do with each ot=
her beyond having the word "default" in their name.

I would be very much in favor of revisiting Interface Default Methods, t=
hough, as I think it would be a strong feature, and it's one found in ne=
arly all of our sibling languages at this point.  PHP is weird for not h=
aving them.  I believe that would also be a better approach than traits =
that link to interfaces, which achieves not-quite the same result with m=
ore steps.

> *Opt-In Feature Flag:* To address backward compatibility concerns, I=20
> propose the introduction of a feature flag, such as=20
> `declare(default_methods =3D 1);`, that could be applied when=20
> implementing an interface or when an interface extends another. This=20
> approach would allow developers to opt-in explicitly, preventing=20
> unintended BC breaks and ensuring that the feature is adopted carefull=
y=20
> and intentionally.

Dear god no, no more feature flags.  Remember that those affect a *file*=
, not a *class*, which are not necessarily 1:1. =20

Besides, there's an opt-in way to skip the defaults already: Implement y=
our own version.  The argument that "an interface author might introduce=
 a new method with a default that is not quite perfect for every impleme=
nting class" never carried much weight with me, as it's a fringe edge ca=
se, unlikely to happen, and easily resolvable when it does.  (Via the ex=
act same means as adding a new method without a default implementation.)

> *Complexity and Developer Experience:* While the feature could=20
> significantly improve the developer experience, it also introduces=20
> complexity in how interfaces are used. To alleviate this, the `default=
`=20
> keyword could be explicitly used to mark default methods, making it=20
> easier for developers to understand and manage. For example:
>
> interface I1 { =20
> `    default public function foo(): int { =20
>         return \PHP_INT_MAX; =20
>     } =20
> }
> `
> This explicit marking not only clarifies the intention behind the=20
> method but also aids in distinguishing between regular and default=20
> methods, simplifying the mental model required to work with interfaces.

How?  All I see here is another keyword I have to use.  It doesn't do an=
ything extra for me as a consumer of that interface.  It doesn't add dis=
ambiguation to the language, either for the engine or human readers (as =
the presence of the body does that already).  It just gives me 8 more ch=
aracters to type. =20



> Just thinking out loud here - looking forward to hearing some thoughts.

My thought is we should just pass Default Interface Methods and be done =
with it. :-)

--Larry Garfield