Re: bikeshed: Typed Aliases
| From: | Mike Schinkel | Date: | Sat, 07 Sep 2024 12:42:21 +0000 |
| Subject: | Re: bikeshed: Typed Aliases | ||
| References: | 1 2 3 4 5 6 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-125466@lists.php.net to get a copy of this message | ||
> On Sep 6, 2024, at 4:45 PM, Larry Garfield <larry@garfieldtech.com> wrote:
> Aliases can then be used only in parameter, return, property, and instanceof types. Extends
> and implements are out of scope entirely.
Is there a strong technical reason why extends and implements should be out of scope?
There is definite utility for this, to create a local alias in a namespace that can be used
throughout the namespace rather than having to refer to the external namespace in many different
places.
> On Sep 6, 2024, at 8:46 PM, Davey Shafik <davey@php.net> wrote:
> I would very much prefer to either go all in with an Enum-like (which means that we can hang
> methods on to the value) or we need to distinguish between type hints for class-likes and type hints
> for not-class-likes (*Bar anyone?).
Allowing methods also have definite value as most use-cases I have seen in other languages alias in
order to add methods, especially for enabling support of interfaces.
Which, however, brings up an important distinction that other languages have made and which I think
PHP would benefit from addressing:
1. Type Alias => Different Name for Same Type
2. Type Def => New Type which has all the same properties and methods of other type
e.g. (being hypothetical with the syntax; bikeshed away):
typealias LocalWidget: Widget
typedef MyWidget: Widget {
function foo() {...}
}
function doSomething(Widget $widget) {...}
$w = new LocalWidget;
doSomething($w); // This works, no problem as LocalWidget === Widget
$w = new MyWidget;
doSomething($w); // This throws an error as MyWidget !== Widget
-Mike