Re: bikeshed: Typed Aliases
| From: | Rowan Tommins [IMSoP] | Date: | Mon, 09 Sep 2024 21:35:13 +0000 |
| Subject: | Re: bikeshed: Typed Aliases | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-125485@lists.php.net to get a copy of this message | ||
On 09/09/2024 19:41, Mike Schinkel wrote:
Referencing prior art (e.g. Go) PHP could allow int literals — e.g.That's an interesting compromise, worth considering.1,47, etc. — to be passed to typedefs derived from ints but require int variables to be typecast to the required type. Same for string literals.
In Go you cannot add or subtract on a typedef without casting to the underlying type. I would definitely prefer that to be relaxed, but only if it is relaxed via an explicit opt-in, e.g. something maybe like this: typedef UserId: int operations: +, -, *, /; typedef UserName: string operations: .;I think this would stray into some of the same complexity as operator overloads on objects, in terms of the types and values allowed. For instance: typedef Metres: int; assert( Metres(2) + Metres(1) === Metres(3) ); // most obvious assert( Metres(2) + 1 === Metres(3) ); // seems pretty clear $_GET['input'] = '1'; assert( Metres(2) + $_GET['input'] === Metres(3) ); // might be more controversial typedef Feet: int; assert( Metres(2) + Feet(1) === Metres(3) ); // almost certainly a bad idea Not unsolvable, but probably enough scope for nuance and debate that it should be left well into Future Scope.
type MyNewType: Foo type MyAlias = FooI know this was only an example, but as a general point, I think we should avoid concise but cryptic differences like this. PHP is generally keyword-heavy, rather than punctuation-heavy, and I think that's a valid style which we should keep to. -- Rowan Tommins [IMSoP]