Re: [PHP-DEV] Object Type Casting Reloaded

2019-04-24 Thread Michał Brzuchalski
wt., 23 kwi 2019 o 11:17 Nikita Popov napisał(a): > ... > Without commenting on the rest of the proposal: It's not possible to use > (ClassName) as a cast syntax, because it is ambiguous. For example (Foo) > [$x] is already valid syntax (fetch constant Foo and take index $x), or > (Foo) +$bar, et

Re: [PHP-DEV] Object Type Casting Reloaded

2019-04-23 Thread Stephen Reay
> On 23 Apr 2019, at 21:54, Benjamin Morel wrote: > > @Stephen Reay > >> In languages without type hinting (e.g. JavaScript) - the “boiler plate” > you’re trying to remove is also common, e.g. using `typeof` in JavaScript. > > It's also very common to *not* check types in JavaScript... I ass

Re: [PHP-DEV] Object Type Casting Reloaded

2019-04-23 Thread Benjamin Morel
@Stephen Reay > In languages without type hinting (e.g. JavaScript) - the “boiler plate” you’re trying to remove is also common, e.g. using `typeof` in JavaScript. It's also very common to *not* check types in JavaScript... I assume the absence of type hints is partly responsible for this, and a

Re: [PHP-DEV] Object Type Casting Reloaded

2019-04-23 Thread Stephen Reay
> On 23 Apr 2019, at 21:02, Benjamin Morel wrote: > > It's exactly the purpose of the proposal: avoid this boilerplate. For the > same reason you probably write: > Replacing 1 line of “boiler plate” with a *different* line of less-obvious “boiler plate” is not an improvement IMO. > The cur

Re: [PHP-DEV] Object Type Casting Reloaded

2019-04-23 Thread Benjamin Morel
@Stephen Reay > And as I said before, I can’t understand how that is supposed to be better than just doing what works now, using instanceof to check the type inside the block: > if (! $foo instanceof Foo) { throw new TypeError(…); } > At least with the above, it’s obvious what is happening. It's

Re: [PHP-DEV] Object Type Casting Reloaded

2019-04-23 Thread Mark Randall
On 23/04/2019 14:39, Mark Randall wrote: It keeps coming back to the feature we've all been praying to the PHP Gods for... generics. Replying to myself (sorry). I just wanted to point out that the same syntax would also solve your other issue from the nullable casts thread: function ncast($

Re: [PHP-DEV] Object Type Casting Reloaded

2019-04-23 Thread Stephen Reay
> On 23 Apr 2019, at 19:32, Benjamin Morel wrote: > > I'm sorry, I still don't understand what the issue with foreach is? As I said > before, the proposed syntax does not aim to be used inside the foreach () > parentheses, but rather as an explicit declaration inside the foreach {} > block.

Re: [PHP-DEV] Object Type Casting Reloaded

2019-04-23 Thread Benjamin Morel
@Mark Randall > It keeps coming back to the feature we've all been praying to the PHP > Gods for... generics. > $x = cast($obj); I did not think about using generics for this, but indeed this could work. It's only reasonably verbose, easily implemented in userland, and should be usable both by s

Re: [PHP-DEV] Object Type Casting Reloaded

2019-04-23 Thread Mark Randall
On 23/04/2019 13:32, Benjamin Morel wrote: Aw. I did not think about a possible conflict here, thanks for bringing this up! It keeps coming back to the feature we've all been praying to the PHP Gods for... generics. function cast(object $obj): T { if ($obj instanceof T) { return $obj;

Re: [PHP-DEV] Object Type Casting Reloaded

2019-04-23 Thread Benjamin Morel
@Nikita Popov > It's not possible to use (ClassName) as a cast syntax, because it is ambiguous. Aw. I did not think about a possible conflict here, thanks for bringing this up! @Stephen Reay > My objection to `as` is that it specifically prevents (or makes very confusing) the `foreach` use I

Re: [PHP-DEV] Object Type Casting Reloaded

2019-04-23 Thread azjezz
Hello Andery. yes that's possible, but would need a function for each type, or we can do : https://3v4l.org/6B3hA but this is still not type-safe, as we won't be able to type hint it correctly without using generics + `typename` type that exists in Hack. using generics + `typename` : `functio

Re: [PHP-DEV] Object Type Casting Reloaded

2019-04-23 Thread Andrey Hristov
Hi, On 23.04.19 г. 13:44 ч., azjezz wrote: Hello Dan, I don' think this a problem relating to just one use case, some PHP builtin functions have weird union return types, where static analysis tools would warn you about the return type being `string|bool`, when you are expecting `string`. us

Re: [PHP-DEV] Object Type Casting Reloaded

2019-04-23 Thread azjezz
Hello Dan, I don' think this a problem relating to just one use case, some PHP builtin functions have weird union return types, where static analysis tools would warn you about the return type being `string|bool`, when you are expecting `string`. using type constrain : ``` $foo = substr($foo, 1

Re: [PHP-DEV] Object Type Casting Reloaded

2019-04-23 Thread Dan Ackroyd
HI Benjamin, Similar to the nullable casting idea, you're taking a problem caused by your own code/framework, and then thinking it should be solved in core, when a simple solution is available in user-land. If you changed what you currently have: $service = $diContainer->get('email.service'); t

Re: [PHP-DEV] Object Type Casting Reloaded

2019-04-23 Thread azjezz
Hello Nikita, I would love to hear your opinion on the `as` syntax from Hack, and whether it can be used in PHP the same way or would it be an issue. Cheers, - Saif Sent with ProtonMail Secure Email. ‐‐‐ Original Message ‐‐‐ On Tuesday, April 23, 2019 10:16 AM, Nikita Popov wrote: >

Re: [PHP-DEV] Object Type Casting Reloaded

2019-04-23 Thread Andrey Hristov
Hi, maybe with short closures this will be pretty easy to implement by injecting a short closure that expects the return type to be of the type to be casted and to return it. Something in the like of : $service = (fn(EmailService $s):EmailService => $s ;)($diContainer->get('email.service'))

Re: [PHP-DEV] Object Type Casting Reloaded

2019-04-23 Thread Stephen Reay
> On 23 Apr 2019, at 16:03, Benjamin Morel wrote: > > > 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) > > Note that we already us

Re: [PHP-DEV] Object Type Casting Reloaded

2019-04-23 Thread Nikita Popov
On Mon, Apr 22, 2019 at 11:48 PM Benjamin Morel wrote: > Hi internals, > > I'd like to revive an old discussion > about > object type casting. > > The idea would be to allow (ClassName) casting: > > $service = (EmailService) $diContainer->get('email.servic

Re: [PHP-DEV] Object Type Casting Reloaded

2019-04-23 Thread Benjamin Morel
Hi all, thanks for the valuable feedback! @Stephen Reay: > 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) Note that we already use `as` in o

Re: [PHP-DEV] Object Type Casting Reloaded

2019-04-22 Thread M. W. Moe
Hello, Sebastian yes; interface or abstract have been somehow created for that purpose; even if I find people abusing of those constructs; now if your container is generalized and represents dynamic data; for instance like a Message class it could hold dynamic data types; let say: // abstract pub

Re: [PHP-DEV] Object Type Casting Reloaded

2019-04-22 Thread Sebastian Bergmann
Am 22.04.2019 um 23:47 schrieb Benjamin Morel: These combine into a third advantage: readability. Today's equivalent of the above one-liner could be: /** @var EmailService $service */ $service = $diContainer->get('email.service'); if (! $service instanceof EmailService) {

Re: [PHP-DEV] Object Type Casting Reloaded

2019-04-22 Thread Stephen Reay
> On 23 Apr 2019, at 07:50, azjezz 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/typed ) > > even though `as` is used with `foreach`, i don't think it would be an issue > to use it for somethin

Re: [PHP-DEV] Object Type Casting Reloaded

2019-04-22 Thread azjezz
I think we would be talking about typed variables at that point. ( something you can do with PHP 7.4 http://github.com/azjezz/typed ) 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 fa

Re: [PHP-DEV] Object Type Casting Reloaded

2019-04-22 Thread Marcos Passos
Such a feature would be very helpful to ensure type safety! A big +1! On Mon, Apr 22, 2019 at 19:51 Benjamin Morel 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 i

Re: [PHP-DEV] Object Type Casting Reloaded

2019-04-22 Thread Stephen Reay
> On 23 Apr 2019, at 06:30, azjezz 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](https://protonm

Re: [PHP-DEV] Object Type Casting Reloaded

2019-04-22 Thread azjezz
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](https://protonmail.com) Secure Email. ‐‐‐ Original Message ‐‐‐ On Monda

Re: [PHP-DEV] Object Type Casting Reloaded

2019-04-22 Thread Benjamin Morel
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.

Re: [PHP-DEV] Object Type Casting Reloaded

2019-04-22 Thread Benjamin Morel
Hi Larry, thanks for the early feedback! > First thought: I'm all for easy ways to be more type-explicit, so yay on the concept. Glad to hear that! > Second thought: (...) how many use cases for that are there other than function boundaries, which we already have covered? I chose an example whe

Re: [PHP-DEV] Object Type Casting Reloaded

2019-04-22 Thread azjezz
Hello, Internals. 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. see : https://docs.hhvm.com/hack/expressions-and-operators/as --- when i see : ``` $foo = (Foo) $object; ``` i thin

Re: [PHP-DEV] Object Type Casting Reloaded

2019-04-22 Thread Larry Garfield
On Mon, Apr 22, 2019, at 4:47 PM, Benjamin Morel wrote: > Hi internals, > > I'd like to revive an old discussion > about > object type casting. > > The idea would be to allow (ClassName) casting: > > $service = (EmailService) $diContainer->get('email.ser