Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:115407 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 17589 invoked from network); 12 Jul 2021 14:03:17 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 12 Jul 2021 14:03:17 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 62F2618033A for ; Mon, 12 Jul 2021 07:26:33 -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.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-yb1-f170.google.com (mail-yb1-f170.google.com [209.85.219.170]) (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 07:26:32 -0700 (PDT) Received: by mail-yb1-f170.google.com with SMTP id i18so29180776yba.13 for ; Mon, 12 Jul 2021 07:26:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=datadoghq.com; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=xmKzW48zjOJ/1OzDlulB7VnBJlOR9ORMQAh5/IjWV2Y=; b=d12WRyj/B5mo4j0EozbYrQi1auHOENCW1y0UZTp60agPiYLlBUGaZLuJk17enK52lM tl9a6x1wvhjhlxar1JoMg5RG1AK+7D1tk35JFZbK4LywxeoBkyIsRxK/pDsApOOT5kpJ Ipe27roui+UwD/rl66Qr1fn25gu7rHdOn+rck= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=xmKzW48zjOJ/1OzDlulB7VnBJlOR9ORMQAh5/IjWV2Y=; b=F8ClMDTdWpki3c0heuWIJf5qn2oAw0dvPIIBVYYJVBQWU7M5sK5Sy/E1HPg5dynyn1 TV7xQ7SvKMnd+xVfanBrOrHqYum2/Ajq7zw1uFm7ideycnL49h4lqb3fEzIxFAyE5PuT zRC4p/2+Dim5unTYGVCDusV6wG1EJOANJ63sI24n7WbRh6q9QKctH/CQeDdYbyJ7rpxB WAEPsWlUzT1otb5PWouoFGCiu03fpbfvgNYPQqWoOh8R37pqr+g2oZROWJwfcxS/I2k+ MuhEqnMOdKPErdZ+YdrZxD1qYiTkVIsddZ86dZc/CMkQ8A7YXSayOZ5jARub4j8cwDcj aq5w== X-Gm-Message-State: AOAM530JhNaeZnW1l4Hss97pSt0ZbTMsnv3OZpU156N39mYfJBo4YZpF oeW3W9kHanfRQIAK9CWmtJw/mkJPmUSi9ExGSvI3yg== X-Google-Smtp-Source: ABdhPJync77JxUIMKwR/IVUodGbNp1mxU0ZbjT08cvu4zLROSrQ3GFwpLfuGwx64CUfeRr+5bOrCXd5XOpHxAp/S8bc= X-Received: by 2002:a25:af8f:: with SMTP id g15mr27174301ybh.85.1626099988136; Mon, 12 Jul 2021 07:26:28 -0700 (PDT) MIME-Version: 1.0 References: <0d28ee72-accd-4dfc-a8e0-018c99e4a86d@www.fastmail.com> In-Reply-To: <0d28ee72-accd-4dfc-a8e0-018c99e4a86d@www.fastmail.com> Reply-To: Levi Morrison Date: Mon, 12 Jul 2021 08:26:17 -0600 Message-ID: To: Larry Garfield Cc: php internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Type casting syntax From: internals@lists.php.net ("Levi Morrison via internals") On Sun, Jul 11, 2021 at 5:19 PM Larry Garfield wro= te: > > On Sun, Jul 11, 2021, at 5:30 PM, Levi Morrison via internals wrote: > > I like Rust's From and TryFrom traits, which allow types to define > > conversions into or from other types. The From trait always succeeds, > > and TryFrom can fail. However, it's not a "cast" -- the user calls an > > `into`/`try_into` or `from`/`try_from` methods. > > > > I would be supportive of defining official ways to convert types from > > one to another, not necessarily casts, though. I think these qualities > > are important: > > > > 1. Would have failable and non-failable variants. It's nice to know > > that a particular conversion cannot or can fail. > > 2. It should be amenable to static analysis. Tools should be able to > > warn about missing paths for handling failable conversions, or that > > conversion from A to B is possible but A to C is not. > > > > I'm not sure how to technically achieve this for PHP. I don't think > > any casting proposal I've seen meets these characteristics, so I would > > personally vote no on them. > > What are the use cases for integrated object to object (ie, class to clas= s) 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 sibli= ngs 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.