Re: [PHP-DEV] Inline typecasting / typehinting for classes and interfaces

2012-08-18 Thread André Rømcke
( resending with correct formatting, and missing context while at it, sorry about that ) On Aug 15, 2012, at 8:33 PM, Nikita Popov wrote: (...) Another aspect here is that there is no reasonable syntax for this feature, at least I can't think of one: * The syntax `$foo = (InterfaceName)

Re: [PHP-DEV] Inline typecasting / typehinting for classes and interfaces

2012-08-17 Thread André Rømcke
On Aug 15, 2012, at 8:33 PM, Nikita Popov wrote: On Wed, Aug 15, 2012 at 8:15 PM, Kris Craig kris.cr...@gmail.commailto:kris.cr...@gmail.com wrote: On Wed, Aug 15, 2012 at 4:48 AM, Anthony Ferrara ircmax...@gmail.commailto:ircmax...@gmail.comwrote: Stan, On Wed, Aug 15, 2012 at 3:57 AM, Stan

Re: [PHP-DEV] Inline typecasting / typehinting for classes and interfaces

2012-08-16 Thread Sebastian Krebs
Hi, after reading this mail: Is it just me or is a userspace implementation really that trivial? function ensure($object, $class) { if ($object instanceof $class) { return $object; } throw new InvalidArgumentException(sprintf('Object is not of type %s', $class)); } And then

Re: [PHP-DEV] Inline typecasting / typehinting for classes and interfaces

2012-08-15 Thread Stan Vass
Hi! I agree with you. The one case where this syntax may be very useful is if we want to implement class casting. So introduce a pair of magic methods I do not think we want to implement class casting. I'm not sure how class casting even makes sense - if the object is of one class, how can

Re: [PHP-DEV] Inline typecasting / typehinting for classes and interfaces

2012-08-15 Thread Stas Malyshev
Hi! My proposal is simple: behave as an inline type hint. The same type hints you have in arguments lists, but inline. The use case is I want to make sure this value is of this type and a side benefit is the IDE can know the variable is of this type too (for autocompletion purposes).

Re: [PHP-DEV] Inline typecasting / typehinting for classes and interfaces

2012-08-15 Thread Stan Vass
I'd like to also ask people to read what the intended effect of the proposal is instead of going into abstract discussions about how casting one class to another doesn't make sense (this is not what's being proposed). Just like PHP typehints look like static typing but *aren't*, the same way

Re: [PHP-DEV] Inline typecasting / typehinting for classes and interfaces

2012-08-15 Thread Stan Vass
What's wrong with instanceof? You can then throw fatal error if you want, it's just two lines: if(!($foo instanceof Bar)) { trigger_error(E_USER_ERROR, Wrong foo!); } That's 3 lines on top of the line where you assign the value, and you forgot the 4th line for the IDE: - /* @var $foo

Re: [PHP-DEV] Inline typecasting / typehinting for classes and interfaces

2012-08-15 Thread Stas Malyshev
Hi! And assignment is a kinda common operation. So I hope you can see what's wrong with it now. No I do not. Not every imaginable use case should have dedicated language construct for it - sometimes it is OK to type 2 lines of code. Sometimes even 3. This case is well served by existing

Re: [PHP-DEV] Inline typecasting / typehinting for classes and interfaces

2012-08-15 Thread Stan Vass
And assignment is a kinda common operation. So I hope you can see what's wrong with it now. No I do not. Not every imaginable use case should have dedicated language construct for it - sometimes it is OK to type 2 lines of code. Sometimes even 3. This case is well served by existing language

Re: [PHP-DEV] Inline typecasting / typehinting for classes and interfaces

2012-08-15 Thread Nikita Popov
On Wed, Aug 15, 2012 at 10:06 AM, Stan Vass sv_for...@fmethod.com wrote: I'd like to also ask people to read what the intended effect of the proposal is instead of going into abstract discussions about how casting one class to another doesn't make sense (this is not what's being proposed). I

Re: [PHP-DEV] Inline typecasting / typehinting for classes and interfaces

2012-08-15 Thread Stas Malyshev
Hi! Let me ask you - do you think the existing PHP typehints are pointless too? Do you feel they don't give you enough flexibility? Do you feel they reinvented a language construct for the purpose of saving the typing of one if clause (per argument) (per method) (per class)? They are not

Re: [PHP-DEV] Inline typecasting / typehinting for classes and interfaces

2012-08-15 Thread Stan Vass
On Wed, Aug 15, 2012 at 10:06 AM, Stan Vass sv_for...@fmethod.com wrote: I'd like to also ask people to read what the intended effect of the proposal is instead of going into abstract discussions about how casting one class to another doesn't make sense (this is not what's being proposed). I

Re: [PHP-DEV] Inline typecasting / typehinting for classes and interfaces

2012-08-15 Thread Stan Vass
Hi! Let me ask you - do you think the existing PHP typehints are pointless too? Do you feel they don't give you enough flexibility? Do you feel they reinvented a language construct for the purpose of saving the typing of one if clause (per argument) (per method) (per class)? They are not

Re: [PHP-DEV] Inline typecasting / typehinting for classes and interfaces

2012-08-15 Thread Stas Malyshev
Hi! All right, your method accepts an array of objects, instances of Foo. How do you enforce this contract? You are trying to re-invent generics/parametrized types and bolt it onto PHP. I still don't think it is a good idea - PHP is not a statically typed language. Just check you array

Re: [PHP-DEV] Inline typecasting / typehinting for classes and interfaces

2012-08-15 Thread Stan Vass
Hi! All right, your method accepts an array of objects, instances of Foo. How do you enforce this contract? You are trying to re-invent generics/parametrized types and bolt it onto PHP. I still don't think it is a good idea - PHP is not a statically typed language. Just check you array

Re: [PHP-DEV] Inline typecasting / typehinting for classes and interfaces

2012-08-15 Thread Nikita Popov
On Wed, Aug 15, 2012 at 11:17 AM, Stan Vass sv_for...@fmethod.com wrote: You're 10 years too late to argue the merits of typehints in PHP. I am not proposing the introduction of typehints. I'm just saying we have the option to use them in arguments, let's have the options inline too. This way

Re: [PHP-DEV] Inline typecasting / typehinting for classes and interfaces

2012-08-15 Thread Stan Vass
Stas already pointed out that parameter typehints allow you to define the interface for your method. Return typehints would server a similar purpose and as such I'd consider them useful. But variable typehints don't serve any such purpose. I gave an example validating an array of Foo instances,

Re: [PHP-DEV] Inline typecasting / typehinting for classes and interfaces

2012-08-15 Thread Stan Vass
But variable typehints don't serve any such purpose. Actually, one could even say that they don't serve *any* purpose, short of providing the IDE with type information, because your code would work just as well even without the type check. If the type were wrong, it would just throw a fatal error

Re: [PHP-DEV] Inline typecasting / typehinting for classes and interfaces

2012-08-15 Thread Nikita Popov
On Wed, Aug 15, 2012 at 12:01 PM, Stan Vass sv_for...@fmethod.com wrote: Just like with argument typehints. Point me to an argument typehint that is required for your code to run. You and Stas keep giving arguments against argument typehints, which is really awkward. As already pointed out

Re: [PHP-DEV] Inline typecasting / typehinting for classes and interfaces

2012-08-15 Thread Adam Harvey
On 15 August 2012 18:15, Nikita Popov nikita@gmail.com wrote: As already pointed out repeatedly, argument typehints serve the purpose of defining an interface. No, they are not required to run the code, that's true. But they still serve an important purpose for object oriented programming

Re: [PHP-DEV] Inline typecasting / typehinting for classes and interfaces

2012-08-15 Thread Sebastian Krebs
2012/8/15 Stan Vass sv_for...@fmethod.com But variable typehints don't serve any such purpose. Actually, one could even say that they don't serve *any* purpose, short of providing the IDE with type information, because your code would work just as well even without the type check. If the

Re: [PHP-DEV] Inline typecasting / typehinting for classes and interfaces

2012-08-15 Thread Anthony Ferrara
Stan, On Wed, Aug 15, 2012 at 3:57 AM, Stan Vass sv_for...@fmethod.com wrote: Hi! I agree with you. The one case where this syntax may be very useful is if we want to implement class casting. So introduce a pair of magic methods I do not think we want to implement class casting. I'm not

Re: [PHP-DEV] Inline typecasting / typehinting for classes and interfaces

2012-08-15 Thread Kris Craig
On Wed, Aug 15, 2012 at 4:48 AM, Anthony Ferrara ircmax...@gmail.comwrote: Stan, On Wed, Aug 15, 2012 at 3:57 AM, Stan Vass sv_for...@fmethod.com wrote: Hi! I agree with you. The one case where this syntax may be very useful is if we want to implement class casting. So introduce a

Re: [PHP-DEV] Inline typecasting / typehinting for classes and interfaces

2012-08-15 Thread Nikita Popov
On Wed, Aug 15, 2012 at 8:15 PM, Kris Craig kris.cr...@gmail.com wrote: On Wed, Aug 15, 2012 at 4:48 AM, Anthony Ferrara ircmax...@gmail.comwrote: Stan, On Wed, Aug 15, 2012 at 3:57 AM, Stan Vass sv_for...@fmethod.com wrote: Hi! I agree with you. The one case where this syntax may be

[PHP-DEV] Inline typecasting / typehinting for classes and interfaces

2012-08-14 Thread Stan Vass
I've felt the need for this for some time. Proposed syntax: - $x = (InterfaceName) $container-service; Proposed behavior: --- Checks if the instance if an instance of the given class/interfaces. If yes, does nothing, if not, issues a fatal

Re: [PHP-DEV] Inline typecasting / typehinting for classes and interfaces

2012-08-14 Thread Kris Craig
On Tue, Aug 14, 2012 at 12:46 AM, Stan Vass sv_for...@fmethod.com wrote: I've felt the need for this for some time. Proposed syntax: - $x = (InterfaceName) $container-service; Proposed behavior: --- Checks if the instance if an instance

Re: [PHP-DEV] Inline typecasting / typehinting for classes and interfaces

2012-08-14 Thread Levi Morrison
On Tue, Aug 14, 2012 at 1:46 AM, Stan Vass sv_for...@fmethod.com wrote: I've felt the need for this for some time. Proposed syntax: - $x = (InterfaceName) $container-service; I'm against this. Let's be honest, how different is this that an optionally static type?

Re: [PHP-DEV] Inline typecasting / typehinting for classes and interfaces

2012-08-14 Thread Stan Vass
Proposed syntax: - $x = (InterfaceName) $container-service; I'm against this. Let's be honest, how different is this that an optionally static type? InterfaceName $x = $container-service; To be clear, I WANT optionally static typing. For the most part, type-hinting

Re: [PHP-DEV] Inline typecasting / typehinting for classes and interfaces

2012-08-14 Thread Anthony Ferrara
Levi, On Tue, Aug 14, 2012 at 9:51 AM, Levi Morrison morrison.l...@gmail.comwrote: On Tue, Aug 14, 2012 at 1:46 AM, Stan Vass sv_for...@fmethod.com wrote: I've felt the need for this for some time. Proposed syntax: - $x = (InterfaceName) $container-service;

Re: [PHP-DEV] Inline typecasting / typehinting for classes and interfaces

2012-08-14 Thread Stas Malyshev
Hi! I agree with you. The one case where this syntax may be very useful is if we want to implement class casting. So introduce a pair of magic methods I do not think we want to implement class casting. I'm not sure how class casting even makes sense - if the object is of one class, how can you