Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:105341 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 16158 invoked from network); 23 Apr 2019 05:29:56 -0000 Received: from unknown (HELO mail1.25mail.st) (206.123.115.54) by pb1.pair.com with SMTP; 23 Apr 2019 05:29:56 -0000 Received: from [10.0.1.62] (unknown [49.48.243.98]) by mail1.25mail.st (Postfix) with ESMTPSA id C918860493; Tue, 23 Apr 2019 02:30:03 +0000 (UTC) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (1.0) X-Mailer: iPhone Mail (16E227) In-Reply-To: Date: Tue, 23 Apr 2019 09:30:00 +0700 Cc: Benjamin Morel , Larry Garfield , "internals@lists.php.net" Content-Transfer-Encoding: quoted-printable Message-ID: <5098BF1F-F49E-4B4E-9A71-9CB3DF637AE3@koalephant.com> References: To: azjezz Subject: Re: [PHP-DEV] Object Type Casting Reloaded From: php-lists@koalephant.com (Stephen Reay) > On 23 Apr 2019, at 07:50, azjezz wrote: >=20 > I think we would be talking about typed variables at that point. >=20 >=20 > ( something you can do with PHP 7.4 http://github.com/azjezz/typed ) >=20 > even though `as` is used with `foreach`, i don't think it would be an issu= e to use it for something else, but i think i will leave that to someone who= 's more familiar with the PHP parser than me. >=20 >=20 > Cheers. >=20 > Sent with ProtonMail Secure Email. >=20 > =E2=80=90=E2=80=90=E2=80=90=E2=80=90=E2=80=90=E2=80=90=E2=80=90 Original M= essage =E2=80=90=E2=80=90=E2=80=90=E2=80=90=E2=80=90=E2=80=90=E2=80=90 > On Tuesday, April 23, 2019 1:43 AM, Stephen Reay wrote: >=20 >>> On 23 Apr 2019, at 06:30, azjezz azjezz@protonmail.com wrote: >>> Hello Ben. >>> yes, i have made a gist with a simple example to show the `as` operator u= sage in hack + HHVM ( 4.1.0 ) output. >>> see : https://gist.github.com/azjezz/03955ff2b009f1ced22ce68c9a862847 >>> Sent with ProtonMail Secure Email. >>> =E2=80=90=E2=80=90=E2=80=90=E2=80=90=E2=80=90=E2=80=90=E2=80=90 Original= Message =E2=80=90=E2=80=90=E2=80=90=E2=80=90=E2=80=90=E2=80=90=E2=80=90 >>>=20 >>>> On Monday, April 22, 2019 11:50 PM, Benjamin Morel benjamin.morel@gmail= .com wrote: >>>> Hi Azjezz, thanks for jumping in! >>>>=20 >>>>> I have been using HackLang for quite a while now and i believe they ha= ve a better solution for this, and it would awesome to see it in PHP, the `a= s` operator. >>>>=20 >>>> If I understand correctly, `as` is an operator that performs type check= s but never casts like () does. >>>> This looks like a serious candidate for an alternative syntax to the on= e I proposed! >>>>=20 >>>> - Ben >>=20 >> I like the idea but I find the syntax (both suggestions) less than great,= given that there=E2=80=99s already =E2=80=9Censure a type=E2=80=9D syntax, a= nd both suggestions are already used for other things (casting and foreach) >>=20 >> Is there some reason (eg ambiguities) a type can=E2=80=99t just be placed= before a variable to ensure a type, similar to a function parameter or type= d property? >>=20 >> Eg >>=20 >> string $foo =3D 'bar'; >> array $bar =3D getBar(); >> foreach($bar as int $k =3D> string $v) {...} >>=20 >> Cheers >>=20 >> Stephen >=20 >=20 I=E2=80=99m talking about exactly the same functionality that exists with me= thod parameters - constraining the type it must conform to when it is declar= ed. Nothing more nothing less. Using `as` means trying to add type constraints to an iterable in the foreac= h would be ridiculously confusing: foreach ( $foo as $K as int =3D> $v as string) {...} And the cast syntax re-use means you=E2=80=99d not be able to use the object= pseudotype (or you=E2=80=99d have to remove casting to object)=20 Cheers Stephen=