First-class types RFC
| From: | Adam Kecskes | Date: | Sat, 09 Nov 2024 08:39:57 +0000 |
| Subject: | First-class types RFC | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-125930@lists.php.net to get a copy of this message | ||
Hello internals,
I propose adding first-class types to PHP, as this feature could greatly enhance the language,
especially given PHP's unique runtime type-checking system. Many libraries currently rely on
class strings as function parameters, which could be made significantly safer with native types
rather than strings. This addition would not only streamline existing code but also create a
foundation for future language extensions. For instance, generic classes could be implemented as
syntactic sugar, which would translate into a class property holding the type parameter(s) for each
instance.
A minimal viable version could allow defining types as expressions, introduce a new 'type'
type, and expand is_a to accept those expressions like:
is_a($object, \ClassOne | \ClassTwo);
An example interface might look like:
interface ContainerInterface
{
public function get(type $type);
}
I’d appreciate your feedback on this idea before proceeding with an RFC or draft PR. Please let me
know your thoughts!
Best Regards,
Adam Kecskes