Re: Typed constants revisited
| From: | Guilliam Xavier | Date: | Wed, 30 Mar 2022 13:50:29 +0000 |
| Subject: | Re: Typed constants revisited | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-117455@lists.php.net to get a copy of this message | ||
Hi Mark,
I have updated the RFC https://wiki.php.net/rfc/typed_class_constants
with
>>> more details and examples from this thread, and updated the RFC status to
>>> Under Discussion. Hopefully the updated RFC helps answer questions and
>>> clarifies what the proposal includes.
>>>
>>
Thanks (I assume that you talked with the original author) -- not sure if
you should have started a new thread with the "[RFC]" tag in the subject?
> I think you should also update the "Supported types" section.
>> Starting with enums, constants can also be objects. Once a good technical
>> solution is found, any object would be supported probably
>> https://wiki.php.net/rfc/new_in_initializers#future_scope
>> I think that only void, never and callable types should not be supported,
>> just like on properties.
>>
>
> I have updated the "Supported types" to list out all types that are
> supported and which types are not supported.
>
A few comments, by subsection:
- **Supported types**: This corresponds to the types currently allowed
for const values (as well as unions thereof) except it [still] doesn't
mention enums (which internally are classes implementing the
UnitEnum
interface, and whose values are ultimately of object type) although
currently allowed. It also doesn't mention mixed (used later in examples).
- **Strict and coercive typing modes**: I didn't understand it on first
read; I had to read the later "Constant values" section and compare both
with
https://wiki.php.net/rfc/typed_properties_v2#strict_and_coercive_typing_modes
and https://wiki.php.net/rfc/typed_properties_v2#default_values
- **Inheritance and variance**: Several occurrences of "Class constants"
should probably be "*Typed* class constants". Also, I still think that
protected bool $isExtinct is *not* a good parallel to `public const bool
CAN_FLY` (see my previous message).
- **Constant values**: You should probably remove the iterable example
now [and maybe add an enum one].
> Constants cannot be objects since objects are mutable, so typed constants
> will not be allowed to be an enum (which is technically an object). A typed
> constant _may_ be an enum value though, so the following example will be
> valid:
>
> ```
> enum Fruit
> {
> case Apple;
> case Banana;
> }
>
> class Colors
> {
> protected const string RED = Fruit::Apple;
> protected const string YELLOW = Fruit::Banana;
> }
> ```
>
This is incorrect, Fruit::Apple is of type Fruit (ultimately
object),
not string. But it is *not* mutable, and *is* allowed as a const value
currently.
Besides, I realized that the RFC is [only] for *class* constants; what
about *namespaced (and global)* constants?
Regards,
--
Guilliam Xavier