On 09/09/2024 19:41, Mike Schinkel wrote:
Referencing prior art (e.g. Go) PHP could allow int literals — e.g. `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.
That's an interesting compromise, worth considering.
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 = Foo
I 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]