Re: Object Type Casting Reloaded

From: Date: Tue, 23 Apr 2019 02:30:00 +0000
Subject: Re: Object Type Casting Reloaded
References: 1 2 3 4 5 6 7  Groups: php.internals 
Request: Send a blank email to internals+get-105341@lists.php.net to get a copy of this message
> On 23 Apr 2019, at 07:50, azjezz <azjezz@protonmail.com> wrote: > > I think we would be talking about typed variables at that point. > > > ( something you can do with PHP 7.4 http://github.com/azjezz/typed9f > ÁËÆAàðãl‘D< ) > > even though as is used with > foreach, i don't think it would be an issue 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. > > > Cheers. > > Sent with ProtonMail Secure Email. > > ‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐ > On Tuesday, April 23, 2019 1:43 AM, Stephen Reay <php-lists@koalephant.com> wrote: > >>> 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 usage in hack + HHVM ( 4.1.0 ) output. >>> see : >>> https://gist.github.com/azjezz/03955ff2b009f1ced22ce68c9a862847 >>> Sent with ProtonMail Secure Email. >>> ‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐ >>> >>>> On Monday, April 22, 2019 11:50 PM, Benjamin Morel benjamin.morel@gmail..com wrote: >>>> Hi Azjezz, thanks for jumping in! >>>> >>>>> I have been using HackLang for quite a while now and i believe they have a >>>>> better solution for this, and it would awesome to see it in PHP, the >>>>> as operator. >>>> >>>> If I understand correctly, as is an operator that >>>> performs type checks but never casts like () does. >>>> This looks like a serious candidate for an alternative syntax to the one I >>>> proposed! >>>> >>>> - Ben >> >> I like the idea but I find the syntax (both suggestions) less than great, given that >> there’s already “ensure a type” syntax, and both suggestions are already used for other things >> (casting and foreach) >> >> Is there some reason (eg ambiguities) a type can’t just be placed before a variable to >> ensure a type, similar to a function parameter or typed property? >> >> Eg >> >> string $foo = 'bar'; >> array $bar = getBar(); >> foreach($bar as int $k => string $v) {...} >> >> Cheers >> >> Stephen > > I’m talking about exactly the same functionality that exists with method parameters - constraining the type it must conform to when it is declared. Nothing more nothing less. Using as means trying to add type constraints to an iterable in the foreach would be ridiculously confusing: foreach ( $foo as $K as int => $v as string) {...} And the cast syntax re-use means you’d not be able to use the object pseudotype (or you’d have to remove casting to object) Cheers Stephen

« previous php.internals (#105341) next »