Re: [RFC Discussion] Typed Properties
| From: | Björn Larsson | Date: | Wed, 30 Mar 2016 22:10:30 +0000 |
| Subject: | Re: [RFC Discussion] Typed Properties | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-92036@lists.php.net to get a copy of this message | ||
Den 2016-03-30 kl. 05:16, skrev Joe Watkins:
Morning Dmitry,believe that it's possible to set type for a static property.1) static typed properties are prohibited. why?Feels like that's a separate feature, static properties are as good as makes no difference, global variables. Instance properties, the engine has good control over their manipulation, for static properties it doesn't, it's not impossible, but feels separate. Good that it's clarified in the RFC since one could easily
is a value in having the same rule for type.2) The handling of multiple properties in the same declaration statementis inconsistent. This feels consistent to me .. in other languages where the type is required, it makes sense to assume the type is implied. In a language where the type is optional, public int $foo, $bar; feels ambiguous to me. Given that public is implied for all properties above there
Anyway, in Hack following syntax passes: https://3v4l.org/3tUu93) We already have nullable arguments without any special syntax. Weshould reuse the similar approach for properties. Making properties implicitly nullable defeats the object of trying to provide type safety. Null is never a valid value for a typed property, if it were, you would never be sure of the type of variable you are getting, and would have to litter your code with is_null checks. Maybe good to clarify difference towards default parameters?
Maybe a bit affected by programming in Java recently, having a slightly more positive attitude towards default values ;-) Regards //Björn LarssonI think it might be better to implicitly initialize them according totype (if default value is not provided): bool by false, int by 0, double by 0.0, string by "" (interned), array by [] (immutable), objects by NULL (always nullable). Definitely not, lying about the default value isn't a good idea. There are times when 0 is as valid as 1, or any other value for an int. If you have declared that you know the type of the property, and you write code that accesses that property before there is any possible chance you have set the property, that's a programming error. We should not hide that error with a default value, or by allowing the engine to return null. Don't have a strong opinion on this one, can see both views.