Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:115412 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 34184 invoked from network); 12 Jul 2021 16:38:12 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 12 Jul 2021 16:38:12 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 1FED9180089 for ; Mon, 12 Jul 2021 10:01:28 -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=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2, SPF_HELO_NONE,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-qk1-f181.google.com (mail-qk1-f181.google.com [209.85.222.181]) (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 10:01:27 -0700 (PDT) Received: by mail-qk1-f181.google.com with SMTP id m68so3185522qke.7 for ; Mon, 12 Jul 2021 10:01:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=newclarity-net.20150623.gappssmtp.com; s=20150623; h=from:message-id:mime-version:subject:date:in-reply-to:cc:to :references; bh=GoObFQemakzrnSGNHFDad/nbVhH9KFKudFW6WQ5Av1s=; b=rB8E3/ZNDsGCXYYQ168m/i1w+7/6i2U0cFL7YIQk+Qvv8CP/WxFMlGKegwn9E19dQ8 S3w1/IxIuFiAef6FSg0wq5Uk9mskz4tjFKRpEUYZ8oWjTX/glB4W6DFqbIcsYmQSWVlh ZnrRaKGzx2yqQ05wZPxhVXyjGQd6H8k4Gb4PsGJT0Ux2l/K7RVFAS58YF4Yk3kwj8LXQ mXJWoCm5hhKbsNzKQ9zYlWMvZCm6y1XeWu3Z0HuJKGhDEent7PiuOkpXPfi+LW8dcQBD 8Mf/niMxBU7OnpDxMTiMhRQ9QVY/KvecmCC0tSH3Eu8K4EOo4TGsS68w7x6dh2IMjwX4 oBxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:message-id:mime-version:subject:date :in-reply-to:cc:to:references; bh=GoObFQemakzrnSGNHFDad/nbVhH9KFKudFW6WQ5Av1s=; b=YB63WHfGGrRW/Fx0lt7FexoxmczjY6sg/9P14pugEDs/rWdSgwKYkz/MuwuLT6ElOt pq6vwU17vLwz0iPJ0fOrije9cZ7qRJtFDgFC8QpewwmYeB83TvfSs6+ydBgx3iYU11i9 fdklEmg8XX4FuhTt8XsNY6B//B4WdFFKDnkVz3zCfgXYHjwNLnHM5Gd004GvtXa1jlD5 RnoTBrxiojX5xf2WLKiD9vKo2kaVTielhRNpbE97mDTqez9dnlgwVxE9JkQBSIdFj5Uh AREAYz9o1V4wIItW/zigzKOZo4kdwVZrz5GSYCWNMqPB9PlKVy+vh1XRQPiwZzP6aS2S u/lg== X-Gm-Message-State: AOAM533sAvBonkpMg7iVkj1JswFKwkE5w0gRQsajTgfAYOPPouCgYXYB Jwuqiyuwo+y83BDxBOWyefWFwTxN+A3KVtpi X-Google-Smtp-Source: ABdhPJx66O76lmcjhkpVJbtEiI4NDVJddHdZgYhk3wC1f+wTgWk86DUwTbj4t7pouftlPP6rtEKZTQ== X-Received: by 2002:ae9:ef54:: with SMTP id d81mr579276qkg.234.1626109286664; Mon, 12 Jul 2021 10:01:26 -0700 (PDT) Received: from [192.168.1.10] (c-24-98-254-8.hsd1.ga.comcast.net. [24.98.254.8]) by smtp.gmail.com with ESMTPSA id h6sm5656541qtb.95.2021.07.12.10.01.25 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 12 Jul 2021 10:01:25 -0700 (PDT) Message-ID: Content-Type: multipart/alternative; boundary="Apple-Mail=_31BDF940-E976-43F4-B9AD-C2FD9EEB8CF7" Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.7\)) Date: Mon, 12 Jul 2021 13:01:24 -0400 In-Reply-To: <90547b22-1b12-4cdb-acf5-bde6cace8aad@www.fastmail.com> Cc: php internals To: Larry Garfield References: <0d28ee72-accd-4dfc-a8e0-018c99e4a86d@www.fastmail.com> <90547b22-1b12-4cdb-acf5-bde6cace8aad@www.fastmail.com> X-Mailer: Apple Mail (2.3608.120.23.2.7) Subject: Re: [PHP-DEV] Type casting syntax From: mike@newclarity.net (Mike Schinkel) --Apple-Mail=_31BDF940-E976-43F4-B9AD-C2FD9EEB8CF7 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii > On Jul 12, 2021, at 11:00 AM, Larry Garfield = wrote: > x > On Mon, Jul 12, 2021, at 9:54 AM, Max Semenik wrote: >=20 >> 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^ >>=20 >> $service =3D (EmailService) $diContainer->get('email.service'); >>=20 >> Instead of >>=20 >> /** @var EmailService $service */ >> $service =3D $diContainer->get('email.service'); >> if (!$service instanceof EmailService) { >> throw new TypeError('Expected instance of EmailService, ...'); >> } >=20 > 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)`. Regarding prior art on type assertion, the syntax Go uses is = `value.(type)` so using a similar approach in PHP might look like this = (I'm spitballing by using the double colon as a sigil but it could = anything that doesn't conflict with existing usage, whatever those = options are): $service =3D $diContainer->get('email.service')::(EmailService); Additionally in Go a type assertion can return a second value which is = boolean telling if the type assertion succeeded. Not having this would = effectively moot the benefit to a type assertion if you had to wrap with = try{}catch{} in case it failed. $service, $okay =3D $diContainer->get('email.service')::(EmailService); if (!$ok) { echo 'Not an EmailService.'; } #fwiw -Mike= --Apple-Mail=_31BDF940-E976-43F4-B9AD-C2FD9EEB8CF7--