Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:115410 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 23182 invoked from network); 12 Jul 2021 14:37:54 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 12 Jul 2021 14:37:54 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 3BC2F1804C4 for ; Mon, 12 Jul 2021 08:01:09 -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.6 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL, SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from wout2-smtp.messagingengine.com (wout2-smtp.messagingengine.com [64.147.123.25]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 12 Jul 2021 08:01:08 -0700 (PDT) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.west.internal (Postfix) with ESMTP id A512A3200908 for ; Mon, 12 Jul 2021 11:01:07 -0400 (EDT) Received: from imap43 ([10.202.2.93]) by compute1.internal (MEProxy); Mon, 12 Jul 2021 11:01:07 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm3; bh=GwyLKQ 5CqsQSQg2cOdWMp3n83Pt30+sMTNB0ql+/EZY=; b=l2aSp7Tvy5Xx0L4/QangQH MX3Id12YvDfHLkyDesAbBz9tpB0iD8ZIha3zdoFrCpA/YbveVzRs9QeSoGtsFJT/ s8Vt4cUSaTzBrNTAd31O0uzpCX+V1MTIWnIfSYd5mpvdi65LiZSVLuSrg87aOdI1 wTUk904OQQh3ERtZNYVrZQtr564ft6X5NIKlLTfcMuGgv0qXLyYrQ/0KKPTAYxwJ NNpA7pVTg4X6WQYmfAWOacE1Bsn4fC7uEApNfWM5DWkFzPH1dGqQRtxHSHbvHlgX yrDRFvjXgsKhOTEOUlwHW7CJtVvWHHOUERDOme1AAEphsju6RCMLN4caIeD6d4uA == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvtddruddvgdekudcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefofgggkfgjfhffhffvufgtsehttdertderredtnecuhfhrohhmpedfnfgrrhhr hicuifgrrhhfihgvlhgufdcuoehlrghrrhihsehgrghrfhhivghlughtvggthhdrtghomh eqnecuggftrfgrthhtvghrnhepveehhedvveejledvvefgleevffdtjeekledvkeegheff gfeivdejhffhledtudetnecuffhomhgrihhnpehphhhprdhnvghtnecuvehluhhsthgvrh fuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheplhgrrhhrhiesghgrrhhfihgv lhguthgvtghhrdgtohhm X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id EA0DBAC0081; Mon, 12 Jul 2021 11:01:06 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.5.0-alpha0-531-g1160beca77-fm-20210705.001-g1160beca Mime-Version: 1.0 Message-ID: <90547b22-1b12-4cdb-acf5-bde6cace8aad@www.fastmail.com> In-Reply-To: References: <0d28ee72-accd-4dfc-a8e0-018c99e4a86d@www.fastmail.com> Date: Mon, 12 Jul 2021 10:00:45 -0500 To: "php internals" Content-Type: text/plain Subject: Re: [PHP-DEV] Type casting syntax From: larry@garfieldtech.com ("Larry Garfield") On Mon, Jul 12, 2021, at 9:26 AM, Levi Morrison via internals wrote: > > What are the use cases for integrated object to object (ie, class to class) conversion in PHP? Rust's type system is a very different beast, so its casting logic makes more sense there. I'm not entirely clear on what the use case is in PHP. When would that be superior to "just write an asFoo() method and move on with life"? > > > > Scalar conversion I can see, but every time someone suggests adding siblings to __toString(), there's major pushback. > > > > --Larry Garfield > > > > -- > > PHP Internals - PHP Runtime Development Mailing List > > To unsubscribe, visit: https://www.php.net/unsub.php > > > > The features I was referring to are not casting; casting does exist in > Rust but this is not it. Rather, it's a generic way to convert > objects. It's nice to have a standard way to do it, instead of every > project doing something bespoke. It also can enable some use cases > that aren't relevant in PHP (yet) such as taking any generic type > which can be converted into a T, because you only want to do that > transformation conditionally on something else. So, like David suggested, some standardized version of $foo->as($type), which then returns an instance of $type? I... don't think I've ever done that often enough to justify a standard feature for it. When have you run into it? That also seems quite different from what Max is talking about: On Mon, Jul 12, 2021, at 9:54 AM, Max Semenik wrote: > I was thinking of something akin to many compiled languages' approach of > "consider this expression is now of that type, and throw an exception if > it's not". An example of this approach from Benjamin's proposal of old^ > > $service = (EmailService) $diContainer->get('email.service'); > > Instead of > > /** @var EmailService $service */ > $service = $diContainer->get('email.service'); > if (!$service instanceof EmailService) { > throw new TypeError('Expected instance of EmailService, ...'); > } Hm, that's a different creature. I... would be probably OK with something in that direction, though I wouldn't work on it myself. I think what you're describing here is more of a type assertion. "Assert that this variable is of type X, otherwise bail." So, some kind of non-disableable (or maybe disableable?) shorthand for `assert($foo instanceof Bar)`. --Larry Garfield